Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

dir-fast

1 réponse
Avatar
Méta-MCI \(MVP\)
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--------------------------------------------------

1 réponse

Avatar
Salvatore DI DI0
Salut Michel,

Merci encore pour ta pprestatioj à Pycon ;-)

Cordialement

Salvatore



"Méta-MCI (MVP)" a écrit dans le message
de news: 48334b3b$0$841$
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úlse):
"""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--------------------------------------------------