python 小脚本备份电脑world文档
标签搜索
侧边栏壁纸
  • 累计撰写 4 篇文章
  • 累计收到 3 条评论
python 小脚本备份电脑world文档
Hello World

python 小脚本备份电脑world文档

Bestxiao
2023-11-25 / 0 评论 / 5 阅读 / 正在检测是否收录...

python小脚本

  • 获取电脑所有硬盘
  • 扫描所有的硬盘,使用一个json储存所有的.doc文件和 .docx文件,需要记录文件的名称和文件的绝对路径
  • 将这个json文件储存到脚本同目录下命名为world_rec.json
  • 复制所有world文件到back文件夹

优化

  1. 多线程/多进程扫描: 使用concurrent.futures库来并行扫描文件,以加快速度。
  2. 使用生成器:scan_files函数改为一个生成器,逐步生成文件路径,以便在多线程/多进程环境中逐步处理。

代码实现

import os
import json
from concurrent.futures import ThreadPoolExecutor
import shutil  # 添加 shutil 模块用于文件复制


def get_all_drives():
    drives = [f"{chr(d)}:" for d in range(ord('A'), ord('Z') + 1) if os.path.exists(f"{chr(d)}:")]
    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
0

评论 (0)

取消