1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #!/usr/bin/python
# -*- coding: utf-8 -*-
# Python 3.2
import os, sys, traceback
from fnmatch import fnmatch
from datetime import datetime
def listerepfic(repertoire, selection="*", callback=None):
"""envoie dans callback les sous-répertoires, ainsi que
les fichiers correspondant à la sélection
"""
def erreur(a):
if callback != None:
callback(str(sys.exc_info()[1]).replace(r'\\', '\\' ) + '\n')
tailletotale = 0 # somme des tailles de tous les fichiers sélectionnés
nbfic = 0 # nombre total de fichiers sélectionnés
for rep, _, listefichiers in os.walk(repertoire, onerror=erreur):
for fichier in listefichiers:
if fnmatch(fichier, selection):
chemin = os.path.join(rep, fichier)
taille = os.path.getsize(chemin)
creation = datetime.fromtimestamp(os.path.getctime(chemin)).isoformat('_').replace('-', '/')[:19]
modification = datetime.fromtimestamp(os.path.getmtime(chemin)).isoformat('_').replace('-', '/')[:19]
deracces = datetime.fromtimestamp(os.path.getatime(chemin)).isoformat('_').replace('-', '/')[:19]
if callback!=None:
callback(chr(9).join([rep, fichier, str(taille), creation, modification, deracces])+'\n')
nbfic += 1
tailletotale += taille
return nbfic, int(tailletotale) |
Partager