Annonce

Réduire
Aucune annonce.

Script Python de recherche locale sur vos disques

Réduire
X
 
  • Filtre
  • Heure
  • Afficher
Tout nettoyer
nouveaux messages

  • Script Python de recherche locale sur vos disques

    Bonsoir,

    Je partage mon code souvent utile pour faire deux types de recherche locale sur vos disques
    • une ou plusieurs extensions (option -e)
    • un ou plusieurs noms de fichiers (option -f)


    Exemple d'utilisation

    Code:
    [email protected]:~$ python3 search.py -f /home/fred1599/.eric4 __init__
    /home/fred1599/.eric4/eric4plugins/__init__.pyc
    /home/fred1599/.eric4/eric4plugins/__init__.py
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/__init__.pyc
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/__init__.py
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/ConfigurationPage/__init__.pyc
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/ConfigurationPage/__init__.py
    6 resultat(s)
    Code:
    [email protected]:~$ python3 search.py -e /home/fred1599/.eric4 .py
    /home/fred1599/.eric4/eric4plugins/__init__.py
    /home/fred1599/.eric4/eric4plugins/PluginProjectDjango.py
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/DjangoLoaddataDataDialog.py
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/Project.py
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/LexerDjango.py
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/DjangoDialog.py
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/__init__.py
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/Ui_DjangoDialog.py
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/Ui_DjangoLoaddataDataDialog.py
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/DjangoDumpdataDataDialog.py
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/Ui_DjangoDumpdataDataDialog.py
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/ConfigurationPage/DjangoPage.py
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/ConfigurationPage/__init__.py
    /home/fred1599/.eric4/eric4plugins/ProjectDjango/ConfigurationPage/Ui_DjangoPage.py
    14 resultat(s)
    Pas de dépendances spécifiques, si ce n'est pour windows, d'avoir python...

    Le lien

    Le code

    Code:
    import os
    from sys import argv, exit
    
    class Search:
        def __init__(self, path):
            self.path = self.checkDirectory(path)
            self.files = [info for info in self.init()]        
            self.ListSearchExt = []
            self.ListNames = []
    
        def checkDirectory(self, path):
            if os.path.isdir(path):
                return path
            exit('Error path argument')
    
        def searchExt(self, *extensions):
            self.ListSearchExt = []
            for p, f in self.files:
                fLower = f.lower()
                name, ext = os.path.splitext(fLower)
                if ext in extensions:
                    path = os.path.join(p, f)
                    self.ListSearchExt.append(path)
    
        def displayListExt(self):
            for result in self.ListSearchExt:
                print(result)
            self.displayNumberResult(self.ListSearchExt)
    
        def searchFile(self, *names):
            self.ListNames = []
            for p, f in self.files:
                fLower = f.lower()
                name, ext = os.path.splitext(fLower)
                if name in names:
                    path = os.path.join(p, f)
                    self.ListNames.append(path)
    
        def displayListNames(self):
            for result in self.ListNames:
                print(result)
            self.displayNumberResult(self.ListNames)
    
        def displayNumberResult(self, List):
            print('{} resultat(s)'.format(len(List)))
    
        def init(self):
            for root, dirs, files in os.walk(self.path):
                for f in files:
                    yield (root, f)
    
    
    option, path = argv[1:3]
    arguments = argv[3:]
    
    s = Search(path)
    
    OPTIONS = {'-f': s.searchFile, '-e': s.searchExt}
    DISPLAY = {'-f': s.displayListNames, '-e': s.displayListExt}
    
    if option in OPTIONS and arguments:
        OPTIONS[option](*arguments)
        DISPLAY[option]()
    else:
        exit('Error option does not exist or not argument(s) of search')
    Bonne soirée,
    Dernière modification par fred, 04 janvier 2014, 22h18. Motif: Ajout du code source
Chargement...
X