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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| import os import pandas as pd import sys
if len(sys.argv) < 3: print("Usage python getFile.py {image path} {file/floder}") sys.exit() root_path = sys.argv[1].replace('\\', '/') type_f = sys.argv[2] if type_f != 'file' and type_f != 'floder': print("Usage {file/floder}") sys.exit()
File_list = [] Primary_list = [] Secondary_list = [] Count = [] Floder = [] name = root_path.split('/')[-1] for dirpath, dirnames, files in os.walk(root_path): if not files: continue if dirpath == root_path: continue dir_Primary, dir_Secondary = dirpath.split('/')[-2:] for file in files: File_list.append(file) Primary_list.append(dir_Primary) Secondary_list.append(dir_Secondary) Floder.append(dir_Secondary) Count.append(str(len(files)))
if type_f == 'file': print(f'文件数量 {len(File_list)}') content_dict = { 'PrimaryFolder': Primary_list, 'SecondaryFolder': Secondary_list, 'Filename': File_list, } df = pd.DataFrame(content_dict) df.to_csv(os.path.join(root_path, f'{name}_file.csv'), encoding='utf_8_sig') elif type_f == 'floder': print(f'文件夹数量 {len(Floder)}') content_dict = { 'Floder': Floder, 'Count': Count, } df = pd.DataFrame(content_dict) df.to_csv(os.path.join(root_path, f'{name}_floder.csv'), encoding='utf_8_sig')
|