python:programmes:find
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
python:programmes:find [2019/07/28 17:21] – root | python:programmes:find [2019/07/29 17:27] (Version actuelle) – [os.scandir] root | ||
---|---|---|---|
Ligne 26: | Ligne 26: | ||
===== os.scandir ===== | ===== os.scandir ===== | ||
- | C'est la méthode la plus performante: | + | C'est la méthode la plus performante |
+ | Exemple pour trouver les fichiers **suid** d'un répertoire avec **crossmount** activé ou désactivé (option **xdev** de la commande **find**) : | ||
<code python find.py> | <code python find.py> | ||
- | def find(root): | + | def find(path, crossmount=True): |
+ | dev = os.stat(path).st_dev | ||
try: | try: | ||
- | for entry in os.scandir(root): | + | for entry in os.scandir(path): |
if entry.is_dir() and not entry.is_symlink(): | if entry.is_dir() and not entry.is_symlink(): | ||
- | for result in find2(entry.path): | + | |
+ | continue | ||
+ | | ||
yield result | yield result | ||
yield entry | yield entry | ||
Ligne 41: | Ligne 45: | ||
print(e, file=sys.stderr) | print(e, file=sys.stderr) | ||
- | { print(f.path) for f in find('/ | + | # Search suid files |
+ | for suid in (f.path for f in find('/ | ||
+ | print(suid) | ||
</ | </ | ||
+ | Recherche les fichiers qui ont un **// | ||
< | < | ||
$ ./find.py | $ ./find.py | ||
Ligne 59: | Ligne 66: | ||
- | ===== os.scandir | + | ===== os.listdir |
C'est la méthode la moins performante: | C'est la méthode la moins performante: | ||
Ligne 76: | Ligne 83: | ||
print(e, file=sys.stderr) | print(e, file=sys.stderr) | ||
</ | </ | ||
+ | |||
+ | ===== faster-than-walk ===== | ||
+ | C'est un module externe [[https:// |
python/programmes/find.1564334501.txt.gz · Dernière modification : 2019/07/28 17:21 de root