备份文件脚本
问题描述:
我正在写一个脚本来将文件从一个目录(主)备份到另一个目录(克隆)。 脚本将监视这两个目录。备份文件脚本
如果克隆中的文件丢失,那么脚本会将缺失的文件从主文件复制到 克隆。现在,我在创建丢失的文件夹时遇到问题。
我已经阅读文档,发现shutil.copyfile将创建一个目录,如果 目录不exist.But我越来越显示出目的地DIR 不是exist.Below是代码的IO错误消息。
import os,shutil,hashlib
master="C:\Users\Will Yan\Desktop\Master"
client="D:\Clone"
if(os.path.exists(client)):
print "PATH EXISTS"
else:
print "PATH Doesn't exists copying"
shutil.copytree(master,client)
def walkLocation(location,option):
aList = []
for(path,dirs,files) in os.walk(location):
for i in files:
if option == "path":
aList.append(path+"/"+i)
else:
aList.append(i)
return aList
def getPaths(location):
paths=[]
files=[]
result =[]
paths = walkLocation(location,'path')
files = walkLocation(location,'files')
result.append(paths)
result.append(files)
return result
ma=walkLocation(master,"path")
cl=walkLocation(client,"path")
maf=walkLocation(master,"a")
clf=walkLocation(client,"a")
for i in range(len(ma)):
count = 0
for j in range(len(cl)):
if maf[i]==clf[j]:
break
else:
count= count+1
if count==len(cl):
dirStep1=ma[i][ma[i].find("Master")::]
dirStep2=dirStep1.replace("Master",client)
shutil.copyfile(ma[i],dirStep2)
有谁能告诉我我哪里做错了吗? 谢谢
答
对不起,但文件没有说。这里的full documentation for the function的再现:
shutil.copyfile(src, dst)
复制 内容(没有元数据)命名
src
到名为dst
文件的文件 的。dst
必须是完整的目标文件名; 查看copy()
获取目标目录路径的副本,该副本接受 。如果src
和dst
是相同的文件,Error
是 引发。目标位置必须为 可写;否则,将引发一个IOError
异常。如果dst
已经存在,它将被替换。 特殊文件如字符或 块设备和管道不能使用此功能复制 。src
和dst
是作为字符串给出的路径名。
所以你必须自己创建目录。
使用rsync或robocopy可能会更容易 – Mark 2011-04-09 12:28:16