Outils pour utilisateurs

Outils du site


python:programmes:find

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
python:programmes:find [2012/02/15 20:21] – créée rootpython:programmes:find [2019/07/29 17:27] (Version actuelle) – [os.scandir] root
Ligne 1: Ligne 1:
 ====== Chercher un fichier ou dossier ====== ====== Chercher un fichier ou dossier ======
  
-<code>+===== os.walk ===== 
 + 
 +<code python>
 import os, fnmatch import os, fnmatch
  
Ligne 11: Ligne 13:
             yield os.path.join(path, filename)             yield os.path.join(path, filename)
 </code> </code>
 +
 +
 +===== glob =====
 +
 +<code python>
 +>>> import glob
 +>>> glob.glob('./[0-9].*')
 +['./1.gif', './2.txt']
 +>>> glob.glob('*.gif')
 +['1.gif', 'card.gif']
 +</code>
 +
 +===== os.scandir =====
 +C'est la méthode la plus performante (des builtins modules) en rapidité et en souplesse de code:
 +
 +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>
 +def find(path, crossmount=True):
 +    dev = os.stat(path).st_dev
 +    try:
 +        for entry in os.scandir(path):
 +            if entry.is_dir() and not entry.is_symlink():
 +                if not crossmount and entry.stat().st_dev != dev:
 +                    continue
 +                for result in find(entry.path, crossmount=crossmount):
 +                    yield result
 +                yield entry
 +            else:
 +                yield entry
 +    except OSError as e:
 +        print(e, file=sys.stderr)
 +
 +# Search suid files
 +for suid in (f.path for f in find('/home/gigix', crossmount=False) if f.is_file() and f.stat().st_mode & stat.S_ISUID == stat.S_ISUID)
 +    print(suid)
 +</code>
 +
 +Recherche les fichiers qui ont un **//setuid//** de positionné:
 +<xtermrtf>
 +$ ./find.py
 +/home/gigix/Documents/VSCode-Anywhere/Third-Party/Junest/chroot/usr/lib/dbus-1.0/dbus-daemon-launch-helper
 +/home/gigix/Documents/VSCode-Anywhere/Third-Party/Junest/chroot/usr/lib/polkit-1/polkit-agent-helper-1
 +/home/gigix/Documents/VSCode-Anywhere/Third-Party/Junest/chroot/usr/bin/mount
 +/home/gigix/Documents/VSCode-Anywhere/Third-Party/Junest/chroot/usr/bin/pkexec
 +/home/gigix/Documents/VSCode-Anywhere/Third-Party/Junest/chroot/usr/bin/chfn
 +/home/gigix/Documents/VSCode-Anywhere/Third-Party/Junest/chroot/usr/bin/ksu
 +/home/gigix/Documents/VSCode-Anywhere/Third-Party/Junest/chroot/usr/bin/umount
 +/home/gigix/Documents/VSCode-Anywhere/Third-Party/Junest/chroot/usr/bin/su
 +/home/gigix/Documents/VSCode-Anywhere/Third-Party/Junest/chroot/usr/bin/chsh
 +/home/gigix/Documents/VSCode-Anywhere/Third-Party/Junest/chroot/usr/bin/newgrp
 +</xtermrtf>
 +
 +
 +===== os.listdir =====
 +C'est la méthode la moins performante:
 +
 +<code python>
 +def find_dirs(root_dir):
 +    try:
 +        for entry in os.listdir(root_dir):
 +            entry_path = os.path.join(root_dir, entry)
 +            if os.path.isdir(entry_path) and not os.path.islink(entry_path):
 +                for result in find_dirs(entry_path):
 +                    yield result
 +            else:
 +                yield entry_path                                                                                                                                                              
 +
 +    except OSError as e:
 +        print(e, file=sys.stderr)
 +</code>
 +
 +===== faster-than-walk =====
 +C'est un module externe [[https://pypi.org/project/faster-than-walk/|faster-than-walk]] et est le plus rapide (plus que **os.scandir**).
python/programmes/find.1329337309.txt.gz · Dernière modification : 2012/02/15 20:21 de root