标签 python 下的文章 - Hello World
首页
关于
搜 索
1
波函数坍缩算法生成2D游戏地图
48 阅读
2
新中式宋代美学风格的发展历史 - 工设2101刘某
38 阅读
3
CSS - 在网页中使用自定义字体
14 阅读
4
python 小脚本备份电脑world文档
5 阅读
默认分类
登录
搜 索
标签搜索
python
JavaScript
算法
css
Xiao_no_love
累计撰写
4
篇文章
累计收到
3
条评论
首页
栏目
默认分类
页面
关于
用户登录
登录
找到
1
篇与
python
相关的结果
2023-11-25
python 小脚本备份电脑world文档
python小脚本获取电脑所有硬盘扫描所有的硬盘,使用一个json储存所有的.doc文件和 .docx文件,需要记录文件的名称和文件的绝对路径将这个json文件储存到脚本同目录下命名为world_rec.json复制所有world文件到back文件夹优化多线程/多进程扫描: 使用concurrent.futures库来并行扫描文件,以加快速度。使用生成器: 将scan_files函数改为一个生成器,逐步生成文件路径,以便在多线程/多进程环境中逐步处理。代码实现import os import json from concurrent.futures import ThreadPoolExecutor import shutil # 添加 shutil 模块用于文件复制 def get_all_drives(): drives = [f":" for d in range(ord('A'), ord('Z') + 1) if os.path.exists(f":")] print(drives) return drives def scan_files(drive): files = for root, _, filenames in os.walk(drive): for filename in filenames: if filename.endswith(('.doc', '.docx')): file_path = os.path.join(root, filename) files[filename] = file_path return files def copy_files_to_back_folder(files): # 获取当前脚本所在目录 script_directory = os.path.dirname(os.path.abspath(__file__)) # 构造 back 文件夹路径 back_folder = os.path.join(script_directory, 'back') # 如果 back 文件夹不存在,创建它 if not os.path.exists(back_folder): os.makedirs(back_folder) # 复制文件到 back 文件夹 for filename, file_path in files.items(): destination_path = os.path.join(back_folder, filename) shutil.copy2(file_path, destination_path) def save_json(data): json_data = json.dumps(data, ensure_ascii=False, indent=4) with open('word_rec.json', 'w', encoding='utf-8') as f: f.write(json_data) def scan_and_save(drive): files = scan_files(drive) copy_files_to_back_folder(files) # 调用复制文件的函数 return files if __name__ == '__main__': drives = get_all_drives() with ThreadPoolExecutor(max_workers=len(drives)) as executor: all_files = for drive_files in executor.map(scan_and_save, drives): all_files.update(drive_files) save_json(all_files) print('Word records saved successfully.') pip install shutil
2023年11月25日
5 阅读
0 评论
0 点赞