【练习】检测U盘并自动复制内容到电脑的软件

软件作用:

有U盘插在电脑上后,程序会检测到U盘的路径。

自己可以提前设置一个保存复制文件的路径或者使用为默认保存的复制路径(默认为桌面,可自行修改)。

检测到U盘后程序就会把U盘的文件复制到电脑对应的文件夹内。没有任何提示。

目前应该还存在一些问题,但是简单的使用是可以满足。

本机测试没多大问题。当然我的U盘有限,电脑有限,没经过充分的测试,不确保在其他电脑上可以正常运行。

除开功能,目前已知的问题:

1、当在运行时关闭窗口会出现程序未响应会卡十几秒然后结束掉程序。

2、默认保存路径不显示在界面上

后面会持续优化一下,并打包成exe文件,然后可以设置为开机自启动,这样就不用每次手动操作。

以下是源码:

python 复制代码
import os
import shutil
import psutil
import tkinter
from tkinter import filedialog
 
#author==龙华
 
#主窗口创建
win=tkinter.Tk()
win.title('共享U盘')
win.geometry('300x200')
 
#桌面文件夹浏览,点击浏览可以在电脑上进行选择
def liulan_button():
    filename=filedialog.askdirectory()
    liulan.delete(0,tkinter.END)
    liulan.insert(0,filename)
 
#确定按钮执行动作,确定最终复制路径,启动复制函数
def qdbutton_click():
    fina_path=updata_path()
    copy_files_from_usb(fina_path)
 
#从U盘复制到自己电脑上
def copy_files_from_usb(fina_path):
    usb=get_usb_drive()
    for x in usb:
        usb_path = x
                # 遍历U盘中的文件和文件夹
        for foldername, subfolders, filenames in os.walk(usb_path):
            for filename in filenames:
                # 构建源文件路径和目标文件路径
                source_file = os.path.join(foldername, filename)
                destination_file = os.path.join(fina_path, filename)
                # 复制文件
                shutil.copy(source_file, destination_file)
                print(f"已复制文件:{filename}-{fina_path}")
 
        print("复制完成")
 
#获取u盘路径
def get_usb_drive():
    # 获取所有磁盘分区
    partitions = psutil.disk_partitions()
    li=[]
    # 遍历磁盘分区,查找U盘
    for partition in partitions:
        if 'removable' in partition.opts: #包含removable为U盘
            # 获取U盘路径
            usb_drive = partition.mountpoint[0:2]
            # s=usb_drive[0:2]
            li.append(usb_drive)
    return li
 
#确定最终的路径,如果不填路径则默认新建一个,如果浏览选择则使用新路径
def updata_path():
    new_path=liulan.get()
    if not new_path:
        new_path=moren_path
        if not os.path.exists(new_path):
            os.makedirs(moren_path)
    return new_path
 
#默认路径设置,默认放在桌面
moren_path=tkinter.StringVar()
moren_path.set(r'C:\Users\Administrator\Desktop\共享U盘文件')  #可修改默认路径
moren_path=moren_path.get()#获取默认路径
 
#创建浏览选择窗口
liulan=tkinter.Entry(win, textvariable=moren_path)
liulan.place(x=50,y=55)
 
#创建浏览按钮,绑定浏览函数
liulanButton=tkinter.Button(win,text='浏览',command=liulan_button)
liulanButton.place(x=200,y=50)
 
#创建确定按钮,绑定复制函数
quedingButton=tkinter.Button(win,text='确定',command=qdbutton_click,width=10,height=1)
quedingButton.place(x=80,y=100)
 
#author==longhua
 
# 测试 实例化
if __name__ == '__main__':
    win.mainloop()
    # copy_files_from_usb(quedingButton)
相关推荐
半青年2 小时前
华为鸿蒙电脑能否作为开发机?开发非鸿蒙应用?
ide·华为·编辑器·电脑·idea·harmonyos·visual studio
邓永豪6 小时前
笔记本电脑升级实战手册[3]:扩展内存与硬盘
学习·电脑·硬件·diy·3c硬件
LAM LAB13 小时前
【WIN】笔记本电脑忘记密码解决办法/笔记本电脑重装系统笔记/bitlocker忘记密码的解决办法
笔记·电脑
hgdlip14 小时前
电脑关机再开机会换IP吗?深入解析分配机制
网络协议·tcp/ip·电脑
驱动小百科16 小时前
如何查看电脑处理器配置 电脑处理器查看方法
电脑·电脑处理器怎么看·电脑cpu查看方法·电脑cpu
qq_393828222 天前
Windows ABBYY FineReader 16 Corporate 文档转换、PDF编辑和文档比较
windows·microsoft·电脑·开源软件·软件需求
2501_916589453 天前
Win全兼容!五五 Excel Word 转 PDF 工具解决多场景转换难题
电脑·软件构建·开源软件
文哥工具箱13 天前
办公学习 效率提升 超级PDF处理软件 转换批量 本地处理
电脑·软件构建·开源软件
MOONNIFE3 天前
技嘉主板BIOS升级
电脑
驱动小百科3 天前
如何查看电脑显卡配置参数 一文读懂
电脑·怎么看电脑显卡·电脑显卡·电脑显卡怎么看·电脑显卡配置怎么看