phoemur
(usa Debian)
Enviado em 25/09/2018 - 22:16h
Conforme eu te falei no outro tópico
https://www.vivaolinux.com.br/topico/Python/Selecao-de-arquivos-em-Python
Python acima de 3.5 use os.scandir
abaixo de 3.5 use os.walk
usando os.scandir:
#!/bin/env python3
import os
import sys
''' Extensoes a serem procuradas '''
EXT = ['.txt','.lnk','.application','.veg','.doc','.pdf','.jpg','.gif','.png','.bitmap',
'.mp4','.avi','.zip','.wav','.svg','.mdb','.rar','.tar','.xf','.gz',
'.sqlite3','.mov','.pptx','.pptm','.xlsx','.xlsm','.aes','.accdb','.bmp',
'.mpeg','.sql','.sqlitedb','.jar','.java','.cdr','.vssettings','.vbs','.vssx',
'.cpp','.c','.NET','.rb','.sh','.appref-ms','.html','.css','.sublime-package',
'.bz2','.iso','.img','.sfk','.mkv','.xz','.exe']
def scantree(path):
for entry in os.scandir(path):
try:
if entry.is_dir(follow_symlinks=False):
yield from scantree(entry.path)
else:
filename, extension = os.path.splitext(entry.name.lower())
if extension in EXT:
yield entry
except PermissionError as e: # Não conseguiu entrar no diretorio por falta de permissao
print(str(e))
if __name__ == "__main__":
PY3K = sys.version_info >= (3, 5)
if not PY3K:
raise SystemError("Só funciona com Python >= 3.5")
for entry in scantree(sys.argv[1] if len(sys.argv) > 1 else '.'):
print(os.path.abspath(entry.path))
# os.remove(os.path.abspath(entry.path)) se quiser apagar todos os arquivos descomente esta linha
Usando os.walk (Python < 3.5)
#!/bin/env python
import os
import sys
''' Extensoes a serem procuradas '''
EXT = ['.txt','.lnk','.application','.veg','.doc','.pdf','.jpg','.gif','.png','.bitmap',
'.mp4','.avi','.zip','.wav','.svg','.mdb','.rar','.tar','.xf','.gz',
'.sqlite3','.mov','.pptx','.pptm','.xlsx','.xlsm','.aes','.accdb','.bmp',
'.mpeg','.sql','.sqlitedb','.jar','.java','.cdr','.vssettings','.vbs','.vssx',
'.cpp','.c','.NET','.rb','.sh','.appref-ms','.html','.css','.sublime-package',
'.bz2','.iso','.img','.sfk','.mkv','.xz','.exe']
def scantree(path):
for root, dirs, files in os.walk(path):
dirs = [dir for dir in dirs if not dir.startswith(".")]
for name in files:
if name.startswith("."):
continue
fullname = os.path.join(root, name)
if fullname.startswith("./"):
fullname = fullname[2:]
filename, extension = os.path.splitext(fullname.lower())
if extension in EXT:
yield fullname
if __name__ == "__main__":
for entry in scantree(sys.argv[1] if len(sys.argv) > 1 else '.'):
print(os.path.abspath(entry))
# os.remove(os.path.abspath(entry)) se quiser apagar todos os arquivos descomente esta linha
______________________
https://github.com/phoemur