dir-fast
Sujet: dir-fast De: enleverlesX.XmcX (l' arobase) XmclaveauX.com (Méta-MCI \(MVP\)) Groupes: fr.comp.lang.python
Organisation: les newsgroups par Orange
Date: 20. May 2008, 23:05:32
Bonsoir !
La fonction ci-dessous retourne, très vite, contenu et taille d'un sous-répertoire (récursivement en option) ; sous Win, bien sûr.
@-salutations
--
Michel Claveau
#----------debut------------------------------------------------
import win32file
def dirfast(path, recursif=False):
"""Michel Claveau Informatique
Trouve très vite contenu et taille d'un dossier
Ignore ('saute') . et ..
Retourne :
taille totale
nb fichier
nb sous-dossiers
liste_fichiers
liste_path_des_fichiers
liste_sous-dossiers
liste_path_des_sous-dossiers
"""
try:
items = win32file.FindFilesW(path + "\\*")
except win32file.error, details:
self.errors[path] = str(details[-1])
return
fichiers=[]
fpath=[]
srep=[]
srpath=[]
fdatime=[]
fsize=[]
totalsize = 0
nbfichiers = 0
nbsousrep = 0
for item in items:
#print item
#attr = item[0]
#datetime = item[1]
size = item[-5]
name = item[-2]
if item[0] & 16:
if name != "." and name != "..":
srep.append(name)
srpath.append(path)
nbsousrep += 1
if recursif:
tempsize,tempnbfichiers,tempnbsousrep,tempfichiers,tempfpath,tempsrep,tempsrpath = dirfast(path+'\\'+name,recursif)
totalsize += tempsize
nbfichiers += tempnbfichiers
nbsousrep += tempnbsousrep
fichiers.extend(tempfichiers)
fpath.extend(tempfpath)
srep.extend(tempsrep)
srpath.extend(tempsrpath)
else:
fichiers.append(name)
fpath.append(path)
fdatime.append(item[1])
fsize.append(item[5])
totalsize += size
nbfichiers += 1
return totalsize,nbfichiers,nbsousrep,fichiers,fpath,srep,srpath
if __name__ == "__main__":
tai,nbf,nbs,fl,fp,sl,sp=dirfast(r"D:\Dev", recursif=True) #==> indiquer le bon dossier
print '-'*55
print 'Dossier:',fp[0]
print
print ' Taille :',tai
print ' Nb Fichiers :',nbf
print ' Nb Sous-dirs:',nbs
print '-'*55
#----------fin--------------------------------------------------
| Date | Sujet | | Auteur |
| 01.01. | | | |
|
| actualité |
Un Monty Python sur les traces ...Toutelatele.com - 19 déc 2008Connu en tant que membre des fameux Monty Python, Michael Palin se passionne, depuis, pour le documentaire, et est, notamment, à l'origine de Autour du ... |
|
| source |
|