本帖最后由 silentPasser 于 2021-9-24 11:57 编辑
代码:
[Python] 纯文本查看 复制代码 from multiprocessing import Manager, Pool
import os
def copyFile(oldFile, newFile, queue=None):
with open(oldFile, 'rb') as f:
content = f.read()
with open(newFile, 'wb') as f:
f.write(content)
if queue is not None:
queue.put(1)
def copyFolder(old, new):
os.mkdir(new)
files = os.listdir(old)
pool = Pool(8)
# 待拷贝文件数
n = files.__len__()
queue = Manager().Queue(n)
for file in files:
pool.apply_async(copyFile, (f'{old}\\{file}', f'{new}\\{file}', queue))
pool.close()
# 完成拷贝的文件个数
finish = 0
while finish < n:
finish += queue.get()
rate = int(finish / n * 100)
print('\r', '♦' * int(rate / 5), f'{rate}%', end='')
pool.join()
def main():
old = input("原文件路径:")
new = input('拷贝文件路径:')
if os.path.exists(old):
if os.path.isdir(old):
copyFolder(old, new)
return
copyFile(old, new)
return
print('原文件不存在!')
main()
if __name__ == '__main__':
main()
存在的bug:
要备份的文件夹中不能包含文件夹。 |