问题描述
我有一个服务器位置(源),客户端整天经常在其中提供大量文件。我的任务是将它们复制到另一个服务器位置(目标),并将那些复制的文件从源位置移动到源服务器中的另一个子目录。喜欢:
Before Copy:
Server A
/files
--- /done
--- File1.xml
--- File2.xml
--- File3.xml
Server B
/files
After Copy to Destination Server B and copied files moved to done folder of Source Server A:
Server B
/files
--- File1.xml
--- File2.xml
--- File3.xml
Server A
/files
--- File4.xml
--- /done
--- File1.xml
--- File2.xml
--- File3.xml
我想使这个过程自动化,我已经通过Windows robocopy来实现这一目标。但是在复制文件时,只要客户端提供了File4.xml之类的新文件且未完全复制,我都会遇到问题,也有复制它们的风险。如上所述,如何正确复制和移动文件?两台服务器都在Windows上运行。
解决方法
从“ A”目录,您可以运行以下python脚本,即可完成工作。您可以定义一些时间(请参阅注释部分),以便忽略刚刚添加的文件
import os
import time
from shutil import copy
for i in os.listdir():
if i !='done' and #(time.time()-os.stat(i).st_ctime > define_some_time_limit_to_avoid_copying_recent_files):
copy('source_path_A'+i,'destination_path_B'+i)
copy('source_path_A'+i,'destination_path_A_done'+i)
os.remove('source_path'+i)