介绍
这篇文章给大家介绍利用python怎么实现百度网盘非会员上传超过500个文件,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
- <李>
os.walk()函数,可以树形遍历整个路径下的文件夹列表和文件列表
李> <李>路径(路径).parent属性,可以获取该“路径”的父路径
李> <李>os.path.relpath (“D: \ aaa \ bbb \ ccc",开始=癉: \ aaa")函数,可以返回“bbb \ ccc”字符串,实现路径裁剪。
李> <李>操作系统。9月可以代表任何路径分隔符
李> <李>os.rename()函数,可以实现移动功能
李> <李>系统。argv[1]通过接收“待分割的路径”参数的输入
李>Python面向对象类形式
#, python3.8 #=utf - 8编码 , import 操作系统 import 系统 得到pathlib import 路径 , , class BaiduPanCutter(对象): & # 39;才能& # 39;& # 39;百度网盘500个文件分割器& # 39;& # 39;& # 39; , def 才能;__init__(自我,,root_path,, count=500): ,,,self.root_path =root_path ,,,self.count =,计数 ,,,self.folder_file_dict =,{}, #,文件夹与其文件列表的映射字典 ,,,self.get_folders_files(), #,获取该根路径下的所有文件夹列表和文件列表 , def 才能get_folders_files(自我): ,,,& # 39;& # 39;& # 39;获取该根路径下的所有文件夹列表和文件列表& # 39;& # 39;& # 39; ,,,for 文件夹,,_,,files 拷贝os.walk (self.root_path): ,,,,,self.folder_file_dict(文件夹),=,文件 , def 才能;_split (arr自我,,,,): ,,,& # 39;& # 39;& # 39;分割文件列表,每500算一份& # 39;& # 39;& # 39; ,,,arrs =, [] ,,,while len (arr),祝辞,数: ,,,,,piece =, arr(数): ,,,,,arrs.append(件) ,,,,,arr =, arr[数:] ,,,arrs.append (arr) ,,,return 加勒比海盗 , #,才能分割文件并放到新的文件去 def 才能cut_file(自我): ,,,& # 39;& # 39;& # 39;分割并移动到新的文件夹& # 39;& # 39;& # 39; ,,,for each_folder 拷贝self.folder_file_dict.keys (): ,,,,,num =, 1, #,以500年为倍数,这是1倍 , ,,,,,#,将文件路径(摒弃当前路径)转成字符串,用_隔开 ,,,,,temp_path =, os.path.relpath (each_folder,路径(self.root_path) .parent) ,,,,,temp_path =, temp_path.replace (os.sep,“_") ,,,,,印刷(temp_path) , ,,,,,files_list =, self.folder_file_dict [each_folder] ,,,,,file_group =, self._split (files_list, self.count), #,按500来分割 , ,,,,,if len (file_group),祝辞,1:,#,有超过500个的文件列表 ,,,,,,,for each_group file_group:拷贝,#,遍历每500份的文件列表 ,,,,,,,,,new_folder =, os.path.join (self.root_path, temp_path +,“_", +, str (num)), #,新路径 ,,,,,,,,,if not os.path.exists (new_folder): ,,,,,,,,,,,os.mkdir (new_folder) ,,,,,,,,,for each_file 拷贝each_group: ,,,,,,,,,,,old_file =, os.path.join (each_folder, each_file) ,,,,,,,,,,,new_file =, os.path.join (new_folder, each_file) ,,,,,,,,,,,印刷(“正在将% s 移动到,% s", %, (old_file, new_file)) ,,,,,,,,,,,os.rename (old_file, new_file) ,,,,,,,,,num =, num + 1 ,,,,,:,#,无超过500个的文件列表 ,,,,,,,new_folder =, os.path.join (self.root_path, temp_path), #,新路径 ,,,,,,,if not os.path.exists (new_folder): ,,,,,,,,,os.mkdir (new_folder) ,,,,,,,for each_file 拷贝file_group [0]: # ,,,,,,,,,old_file =, os.path.join (each_folder, each_file) ,,,,,,,,,new_file =, os.path.join (new_folder, each_file) ,,,,,,,,,印刷(“正在将% s 移动到,% s", %, (old_file, new_file)) null null null null null null null null null null null null null null利用python怎么实现百度网盘非会员上传超过500个文件