1 '''
2 version v1.0
3 1. 实现了简单的pycharm运行功能
4 2. 利用追加目录名的方法来实现添加n个目录,但受到目录名长度的限制
5
6 version v2.0
7 1. 利用时间戳表示目录名
8 2. 利用tkinter实现了简单的GUI
9
10 version v3.0
11 1. 修改了部分GUI界面字符
12 2. 利用datetime获取当前时间,并转化为字符串形式添加到目录
13
14 version v4.0
15 1. 修改了部分GUI界面
16
17 version v5.0
18 1. 限定了udisk_path, backup_path和num的范围
19 2. 对以上输入进行容错处理
20 3. 添加了计算程序运行时间的功能
21 4. 添加了输入提示
22 5. 不区分盘符的大小写
23
24 version v6.0
25 1. 更新了使用说明和字体、颜色
26 2. 更新了软件图标
27 3. 解决盘符为G:/的U盘不插入,不会弹出提示的问题。即在异常处理里增加了一个PermissionError的异常。
28
29 version v6.1
30 1. 解决了当U盘盘符为g:/, h:/时,弹出的错误信息
31 2. 给拷贝次数一个默认值1
32 3. 更新了使用说明
33 '''
34
35
36
37 import os, shutil, sys
38 import time, datetime, tkinter
39 import tkinter.messagebox as mb
40
41 class Udisk:
42
43 def __init__(self, backup_path = "D:/", udisk_path = "H:/", num = 1):
44 self.backup_path = backup_path
45 self.udisk_path = udisk_path
46 self.num = num
47
48 def resource_path(self, relative):
49 if hasattr(sys, "_MEIPASS"):
50 return os.path.join(sys._MEIPASS, relative)
51 return os.path.join(relative)
52
53 def copy_GUI(self):
54 root = tkinter.Tk()
55 root.title("U盘拷贝小工具 v6.1")
56 root.geometry("400x260")
57 icopath = self.resource_path(r"favicon-20180501101520441.ico")
58 if os.path.exists(icopath):
59 root.iconbitmap(icopath)
60
61 tkinter.Label(root, text = "U盘盘符(如G:/)", font = ("华文中宋", 12)).grid(row = 0)
62 tkinter.Label(root, text = "拷贝目录(如D:/)", font = ("华文中宋", 12)).grid(row = 1)
63 tkinter.Label(root, text = "拷贝次数(如3)", font = ("华文中宋", 12)).grid(row = 2)
64
65
66 self.input_1 = tkinter.Entry(root)
67 self.input_2 = tkinter.Entry(root)
68 self.input_3 = tkinter.Entry(root)
69
70 self.input_3.insert(10, "1")
71
72 self.input_1.grid(row = 0, column = 1)
73 self.input_2.grid(row = 1, column = 1)
74 self.input_3.grid(row = 2, column = 1)
75
76 tkinter.Button(root, text = "ok", font = ("Arial", 12), background = "MediumSlateBlue", command = self.run).grid(row=3, pady=15, column=0)
77 tkinter.Button(root, text = "exit", font = ("Arial", 12), background = "DarkGray", command = root.quit).grid(row=3, pady=15, column=1)
78
79 tkinter.Label(root, text = "使用说明:\n1.本软件适用于windows系统;\n2.默认以/作为目录分隔符;\n3.目录名不能包含特殊字符\n4.默认拷贝次数为1", justify = "left", font = ("隶书", 12), pady = 20).grid(row = 4)
80
81
82 root.mainloop()
83
84 def dirExists(self):
85 while True:
86 if os.path.exists(self.backup_path) == True:
87 print("copytree方法的路径%s不能是已存在的!" % self.backup_path)
88 now = datetime.datetime.now()
89 self.backup_path += now.strftime('%Y-%m-%d %H-%M-%S.%f')
90 print("程序自动将路径重命名为: %s" % self.backup_path)
91 return self.dirExists()
92 else:
93 break
94
95 def copyUdisk(self):
96 try:
97 self.flag = True
98 file_list = []
99 content = os.listdir(self.udisk_path)
100 self.dirExists()
101 except FileNotFoundError:
102 print("对不起,U盘不存在,请插入U盘重试! ")
103 return False
104 except PermissionError:
105 print("对不起,U盘不存在,请插入U盘重试! ")
106 self.flag = False
107 else:
108 for item_1 in content:
109 if os.path.isfile(os.path.join(self.udisk_path, item_1)) == False:
110 shutil.copytree(os.path.join(self.udisk_path, item_1), self.backup_path + "/" + item_1)
111 else:
112 file_list.append(item_1)
113 for item_2 in file_list:
114 shutil.copy(os.path.join(self.udisk_path, item_2), self.backup_path + "/")
115 print("已拷贝至", self.backup_path)
116 return True
117
118 def run(self):
119 time_1 = time.time()
120 try:
121 self.udisk_path = self.input_1.get()
122 self.backup_path = self.input_2.get()
123 self.num = self.input_3.get()
124 print(self.num)
125 standard_backup_path = ["C:/", "c:/", "D:/", "d:/", "E:/", "e:/", "F:/", "f:/"]
126 standard_udisk_path = ["G:/", "g:/", "H:/", "h:/"]
127 if self.backup_path[0:3] in standard_backup_path and self.udisk_path[:] in standard_udisk_path and (type(int(self.num)) == int or self.num == ""):
128 for i in range(int(self.num)):
129 my_copy = Udisk(self.backup_path, self.udisk_path, self.num)
130 if my_copy.copyUdisk() == False:
131 mb.showerror("错误", "对不起,U盘不存在,请插入U盘重试! ")
132 break
133 elif my_copy.flag == False:
134 mb.showerror("错误", "对不起,U盘盘符不对应,请重试! ")
135 break
136 elif i == (int(self.num) - 1):
137 time_2 = time.time()
138 total_time = time_2 - time_1
139 output = "已拷贝成功:)"
140 mb.showinfo("结果", output)
141 mb.showinfo("共耗时", str(total_time)+"秒")
142 elif self.udisk_path[:] not in standard_udisk_path:
143 if self.udisk_path == "":
144 mb.showerror("输入错误", "U盘盘符不能为空,请重试! ")
145 else:
146 mb.showerror("输入错误", "输入U盘盘符错误,请重试! ")
147 elif self.backup_path[0:3] not in standard_backup_path:
148 if self.backup_path == "":
149 mb.showerror("输入错误", "拷贝目录不能为空,请重试! ")
150 else:
151 mb.showerror("输入错误", "输入拷贝目录错误,请重试! ")
152 except ValueError:
153 mb.showerror("输入错误", "输入拷贝次数错误,请重试! ")
154
155 mycopy = Udisk()
156 mycopy.copy_GUI()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2017-07-04 《Advanced Bash-scripting Guide》学习(十四):HERE Document和cat <<EOF
2017-07-04 《Advanced Bash-scripting Guide》学习(十三):引用变量的两个例子
2017-07-04 《Advanced Bash-scripting Guide》学习(十二):占位符":"及其他