马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import easygui as gui
import os
from threading import Thread
#该代码用于:通过读取一个包含某一目录下文件名的TXT文件,选择目标文件夹,以及选择保存路径(保存路径中不能已经存在TXT中的同名文件),批量复制文件
def get_path_list(id_path,src,target):
with open(id_path) as txt:
id_str = txt.read()
list1 = id_str.split('\n')
id_list = list(filter(None,list1))
src_dir_list = os.listdir(src)
#找出文件夹中所有在TXT中的文件
file_list = list(set(id_list)&set(src_dir_list))
src_path_list = []
target_path_list = []
for file in file_list:
src_path_list.append(os.path.join(src,file))
target_path_list.append(os.path.join(target,file))
return src_path_list,target_path_list
def fail(id_path,src):
with open(id_path) as txt:
id_str = txt.read()
list1 = id_str.split('\n')
id_list = list(filter(None,list1))
src_dir_list = os.listdir(src)
file_list = list(set(id_list)&set(src_dir_list))
fail_list = list(set(id_list)^set(file_list))
if fail_list:
faildownload_path = os.path.dirname(id_path) + '\\faildownload.txt'
gui.msgbox('注意!!!\n详情请查看 faildownload.txt 文件!!!')
with open(faildownload_path,'a') as f:
for each in fail_list:
f.write('\n' + each)
f.write('\n该文件夹中未找到以上图片')
def copy_function(src_path,target_path):
if os.path.isdir(src_path) and os.path.isdir(target_path):
filelist = os.listdir(src_path)
for file in filelist:
path = os.path.join(src_path,file)
if os.path.isdir(path): #判断是否为文件夹
target1 = os.path.join(target_path,file)
os.mkdir(target1) #在目标文件下在创建一个文件夹
copy_function(path,target1)
else:
with open(path, 'rb') as temp:
container = temp.read()
path1 = os.path.join(target_path, file)
with open(path1, 'wb') as f:
f.write(container)
else:
print('复制完毕!')
if __name__ == "__main__":
id_path = gui.fileopenbox('选择Panel ID.txt文件',default="*.txt")
src = gui.diropenbox('图片文件夹路径选择')
target = gui.diropenbox('保存路径选择')
try:
path_list = get_path_list(id_path,src,target)
nums = len(path_list[0])
threads = []
for each in range(nums):
src_path = path_list[0][each]
target_path = path_list[1][each]
os.mkdir(target_path)
t = Thread(target = copy_function,args=(src_path,target_path))
t.start()
threads.append(t)
for thread in threads:
thread.join()
gui.msgbox('复制完成')
fail(id_path,src)
except:
gui.msgbox('复制失败')
|