Table des matières

Chercher un fichier ou dossier

os.walk

import os, fnmatch
 
def locate(pattern, root=os.curdir):
    '''Locate all files matching supplied filename pattern in and below
    supplied root directory.'''
    for path, dirs, files in os.walk(os.path.abspath(root)):
        for filename in fnmatch.filter(files, pattern):
            yield os.path.join(path, filename)

glob

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']

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) :

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)

Recherche les fichiers qui ont un setuid de positionné:

$ ./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

os.listdir

C'est la méthode la moins performante:

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)

faster-than-walk

C'est un module externe faster-than-walk et est le plus rapide (plus que os.scandir).