def get_list_file_size(folder, items): #my_sequence_dict = {"eq": {"foldernaam": "", "sequencenaam" : "","sequencesize":""}} my_sequence_dict = {"sequence": ["", "", ""]} for i, sequence in enumerate(folder): #print("naam folder is {}".format(len(folder[i]))) if len(folder[i]) == 1: pass else: #print("naam daadwerkelijke sequence is {}".format(folder[i])) filesize_list = [] for file in folder[i]: #print(file) #print(path + file) full_path = str(items) + "\\" + str(file) #print(str(path)) #print(str(file)) #print(full_path) #file_size=os.path.getsize(str(full_path)) #file_size = os.stat(full_path) #print('Size of file is', file_size.st_size, 'bytes') #print("the size of " + full_path) # mijn os.stat approach #current_file = os.stat(full_path) #print('Size of file is', os.stat(path).st_size, 'bytes') #filesize_list.append(os.stat(path).st_size) #mijn sys.getsizeof approach """ image_file = Image.open(full_path) print("File Size In Bytes:- "+str(len(image_file.fp.read()))) """ maat = fsutil.get_file_size(full_path) #print("get file size is: " + str(maat)) size_str = fsutil.get_file_size_formatted(full_path) #print("get file size formatted is: " + str(size_str)) filesize_list.append(maat) filesize_totaal = sum(filesize_list) #print(len(filesize_list)) #print(filesize_totaal) #print(humanbytes(filesize_totaal)) #my_sequence_dict ["foldernaam"] = folder my_sequence_dict["sequence"][0] = str(items) my_sequence_dict["sequence"][1] = str(folder[i]) my_sequence_dict["sequence"][2] = humanbytes(filesize_totaal) #print(my_sequence_dict) return my_sequence_dict
def test_create_zip_file(self): zip_path = self.temp_path("archive.zip") f1_path = self.temp_path("a/b/f1.txt") f2_path = self.temp_path("a/b/f2.txt") f3_path = self.temp_path("x/y/f3.txt") f4_path = self.temp_path("x/y/f4.txt") fsutil.create_file(f1_path, content="hello world 1") fsutil.create_file(f2_path, content="hello world 2") fsutil.create_file(f3_path, content="hello world 3") fsutil.create_file(f4_path, content="hello world 4") fsutil.create_zip_file(zip_path, [f1_path, f2_path, f3_path, f4_path]) with self.assertRaises(OSError): fsutil.create_zip_file( zip_path, [f1_path, f2_path, f3_path, f4_path], overwrite=False ) self.assertTrue(fsutil.is_file(f1_path)) self.assertTrue(fsutil.is_file(f2_path)) self.assertTrue(fsutil.is_file(f3_path)) self.assertTrue(fsutil.is_file(f4_path)) self.assertTrue(fsutil.is_file(zip_path)) self.assertTrue(fsutil.get_file_size(zip_path) > 0)
def get_list_file_size(folder): for i, sequence in enumerate(folder): #print("naam folder is {}".format(len(folder[i]))) if len(folder[i]) == 1: pass else: print("naam seq folder is {}".format(folder[i])) filesize_list = [] for file in folder[i]: #print(file) #print(path + file) full_path = str(path) + str(file) #print(str(path)) #print(str(file)) #print(full_path) #file_size=os.path.getsize(str(full_path)) #file_size = os.stat(full_path) #print('Size of file is', file_size.st_size, 'bytes') #print("the size of " + full_path) # mijn os.stat approach current_file = os.stat(full_path) #print('Size of file is', os.stat(path).st_size, 'bytes') #filesize_list.append(os.stat(path).st_size) #mijn sys.getsizeof approach """ image_file = Image.open(full_path) print("File Size In Bytes:- "+str(len(image_file.fp.read()))) """ maat = fsutil.get_file_size(full_path) print("get file size is: " + str(maat)) size_str = fsutil.get_file_size_formatted(full_path) print("get file size formatted is: " + str(size_str)) filesize_list.append(maat) filesize_totaal = sum(filesize_list) print(len(filesize_list)) print(filesize_totaal) print(humanbytes(filesize_totaal))
def test_get_file_size(self): path = self.temp_path("a/b/c.txt") self.temp_file_of_size(path, "1.75 MB") size = fsutil.get_file_size(path) self.assertEqual(size, fsutil.convert_size_string_to_bytes("1.75 MB"))
def list_files(startpath, filter=None, catfunc=None, wbufferfilename=".data.json"): """ liste tous les fichiers (sous-répertoires inclus) à partir de 'startpath' puis filtre suivant les extensions données par 'filter' - exemple ['jpg', 'png'] puis classe les fichiers en fonction de catfunc (défault *) puis écrit un fichier json dans le répertoire scanné (wbufferfilename) """ for root, dirs, files in os.walk(startpath): # remove hidden files = [f for f in files if not f[0] == "."] dirs[:] = [d for d in dirs if not d[0] == "."] # tree level = root.replace(startpath, "").count(os.sep) indent = " " * 1 * (level) output_string = "{}{}/".format(indent, os.path.basename(root)) subindent = " " * 1 * (level + 1) # init dict for json wbuffer = dict() wbuffer["_root"] = { "root": root, "startpath": root.replace(startpath, ""), "level": level, } # cosmetic print print(f"{output_string} {root} {filter}") # scan files for f in files: path = f"{root}/{f}" ext = os.path.splitext(f)[1][1:] file_noext = os.path.splitext(f)[0] # skip if ext not in filter if filter and ext not in filter: continue # get file informations f_date = fsutil.get_file_creation_date(path) f_size = fsutil.get_file_size(path) f_hash = fsutil.get_file_hash(path) # get file category by function and write dict for json if catfunc: cat = catfunc(file_noext, ext) else: cat = "*" _tmp = { "file": f, "ext": ext, "date": f_date, "size": f_size, "hash": f_hash, } if cat in wbuffer: wbuffer[cat].append(_tmp) else: wbuffer[cat] = [_tmp] # cosmetic print(f"{subindent} {ext} > {f} > {f_date} > {f_size} bytes > {f_hash}") # write json wbuffer_file = f"{root}/{wbufferfilename}" with open(wbuffer_file, "w+", encoding="utf-8") as fp: json.dump(wbuffer, fp, cls=DateTimeEncoder) # cosmetic print(f"file {wbuffer_file} saved")