python
Ceci est une ancienne révision du document !
Script pour générer liste de fichiers vidéos
- walk.py
#!/usr/bin/python3 # -*- coding: utf-8 -* #B. Vandeportaele 2017 import os #import encodings #import humanize #sudo pip install humanize ############################################################ #pour affichage des tailles from math import log unit_list = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'] unit_list_decimal= [0, 0, 1, 2, 2, 2] def sizeof_fmt(num): """Human friendly file size""" if num > 1: # exponent = min(int(log(num, 1024)), len(unit_list) - 1) exponent = min(int(log(num, 1024)), len(unit_list) - 1) quotient = float(num) / 1024**exponent unit = unit_list[exponent] num_decimals =unit_list_decimal[exponent] format_string = '{:.%sf} {}' % (num_decimals) return format_string.format(quotient, unit) if num == 0: return '0 bytes' if num == 1: return '1 byte' ############################################################ #gérer le tri alphabétique #pour les videos qui sont dans un sous dossier, il faut que ce soit le nom du dossier qui définisse le début du nom pour le tri ############################################################ class Fichier: def __init__(self,name='',location='',collection='',size=0): self.name = name self.cleanedname = name #compute once to speed up the sorting #change , to . to avoid change of column in the csv file self.cleanedname=self.cleanedname.replace(',','.'); self.cleanedname=self.cleanedname.replace('.',' '); self.cleanedname=self.cleanedname.replace('-',' '); self.cleanedname=self.cleanedname.replace('_',' '); self.cleanedname=self.cleanedname.replace('/',' '); self.cleanednameupper = self.cleanedname.upper() #compute once to speed up the sorting self.location =location self.size = size self.collection=collection def __str__(self): return self.name #redefine "least than <" operator to allow the sorting using uppercase names def __lt__(self,other): if isinstance(other, Fichier): return self.cleanednameupper < other.cleanednameupper else: return NotImplemented ############################################################ #folder_path = "./" folder_path = "/media/freebox/Vidéos/" ma_liste=list() #fich1=Fichier() #fich1.name='efg' #print(fich1.name) #fich2=Fichier() #fich2.name='abc' #print(fich2.name) #ma_liste.append(fich1) #ma_liste.append(fich2) ############################################################ #collection = ['hd','sd','3D'] collection = ['hd','3D'] n=1 while n < 16: directory='/media/HD3TO'+str(n) n=n+1 for col in collection: folder_path = directory+'/'+col+'/' print('------------------------------------------------------------------------') print(folder_path) print('------------------------------------------------------------------------') #https://www.tutorialspoint.com/python/os_walk.htm for root, dirs, files in os.walk(folder_path, topdown=True): # text_file.write(path) # for pat in path: # print(pat) # text_file.write(pat) # for dir in dirs: # print(dir+'/') # text_file.write(dir+'\n') #"Purchase Amount: %s" % TotalAmount) for filename in files: print(filename) filename_noext, file_extension = os.path.splitext(filename) file_extension=file_extension.lower() #if filename!='.DS_Store' : if file_extension=='.avi' or file_extension=='.mkv' or file_extension=='.mp4' or file_extension=='.mov' : #debug pour localiser les fichiers dont l'encodage du nom pose problème en utf8 #print(filename[0:6]) #filenameunicode=filename.encode("utf-8") #print(filenameunicode) #print(filename.decode("utf-8")) #print(os.path.join(root, filename)) # text_file.write(filename+'\n') # ma_liste.append(Fichier(filename,0)) completefilename=os.path.join(root, filename) #remove the folder_path from the name filenamewithdir=completefilename[len(folder_path):] #get the file size filesize=os.path.getsize(completefilename) #add to the list ma_liste.append(Fichier(filenamewithdir,folder_path,col,filesize)) else: print(filename+ ' ignored\n') ############################################################ #print('liste avant tri') #for f in ma_liste: # print(f) ma_liste.sort() #print('liste après tri') #for f in ma_liste: # print(f) ############################################################ text_file = open("OutputList.txt", "w") #print(ma_liste) for f in ma_liste: # text_file.write(f.name+'\t'+str(f.size)+'\t'+f.location+'\n') # text_file.write(f.collection+','+f.cleanedname+','+str(f.size)+','+f.location+','+f.location+f.name+'\n') text_file.write(f.collection+','+f.cleanedname+','+sizeof_fmt(f.size)+','+f.location+','+f.location+f.name+'\n') text_file.close()
python.1510004117.txt.gz · Dernière modification : 2017/11/06 22:35 de bvandepo