renix1

Search files with name

Sep 16th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. # coding:utf-8
  2.  
  3. """ Criação de uma pesquisa funcional no Windows, nas pastas seguintes: C:\\Program Files,
  4. e caso exista C:\\Program Files(x86)
  5. OBS: PESQUISAS QUE NÃO SEJAM PROGRAMAS OU ARQUIVOS DE PROGRAMAS N IRÃO FUNCIONAR
  6. POIS A BUSCA OCORRERÁ APENAS NA PASTA ARQUIVOS DE PROGRAMAS"""
  7.  
  8. from glob import glob  # para pesquisa com *.ext
  9. from os import chdir, listdir  # chdir para ir até o diretório, listdir para listar arquivos dos diretórios (i.e, pastas e arquivos)
  10. from functools import lru_cache as cache
  11.  
  12. # VARIABLE(S) GLOBAL(S), IF EXISTS MORE THAN ONE...
  13. # NOT USED IN THIS PROGRAM.
  14.  
  15. path_master = 'C:\\'
  16. extensions = ['.dll', '.exe', '.ico', '.png', '.']
  17.  
  18. # DEFINE THE CLASS FOR CLEAR THE 'WINDOW'
  19.  
  20. class LimparJanela:
  21.     command = ""
  22.     def __init__(self):
  23.         import sys
  24.         if sys.platform == "win32":
  25.             self.command = "cls"
  26.         else:
  27.             self.command = "clear"
  28.         clear = input("Clear the window (y/n): ")
  29.         if clear.lower() == 'y':
  30.             self.limpar()
  31.         else:
  32.             pass
  33.     def limpar(self):
  34.         import os
  35.         os.system(self.command)
  36.  
  37.  
  38. # REST OF PROGRAM
  39.  
  40. cache(maxsize=None)
  41. def diretorios_possiveis(diretorio_mestre):
  42.     numeros = (i for i in range(0, 10))
  43.     dirs = listdir(diretorio_mestre)
  44.     dirs_available = []
  45.     for item in dirs:
  46.         if '.' in item:
  47.             pass
  48.         elif str(item[0]) in str(numeros):
  49.             pass
  50.         else:
  51.             dirs_available.append(item)
  52.     return dirs_available
  53.  
  54. cache(maxsize=None)
  55. def pesquisa_tudo(diretorios, nome_arquivo):  # diretorios e.g dirs = diretorios_possiveis(path_master)
  56.     global direc
  57.     nome_arquivo = nome_arquivo + '*'
  58.     acessados = []
  59.     achados = []
  60.     try:
  61.         for direc in diretorios:
  62.             if direc in acessados:
  63.                 pass
  64.             else:
  65.                 direc = path_master+direc
  66.                 chdir(direc)
  67.                 # print("Estamos no diretório: %s" % direc)
  68.                 for arquivo in glob(nome_arquivo):
  69.                     #print(arquivo)
  70.                     achados.append(arquivo)
  71.     except FileNotFoundError:
  72.         print("O sistema não pôde encontrar o 'arquivo' especificado")
  73.     except NotADirectoryError:
  74.         print("Isso não é um diretório")
  75.     except PermissionError:
  76.         print("Não temos permissão para acessar esse 'arquivo'")
  77.     return achados
  78.    
  79. dirs = diretorios_possiveis(path_master)
  80. search_input = input("Digite o nome do arquivo de pesquisa: ")
  81. founds = pesquisa_tudo(dirs, search_input)
  82. LimparJanela()
  83. print("\n\nArquivos encontrados: ")
  84. for found in founds:
  85.     print(found)
Advertisement
Add Comment
Please, Sign In to add comment