开发者

Backup File Script

I am writing a script to backup files from one dir(Master) to another dir(Clone). And the script will monitor the two directories.

If a file inside clone is missing then the script will copy the missing file from Master to Clone.Now I have a problem creating the missing folder.

I have read the documentation and found that shutil.copyfile will create a dir if the dir doesn't exist.But I am getting an IOError message showing that the destination dir is not exist.Below is the code.

import os,sh开发者_高级运维util,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)

Can anyone tell me where did I do wrong? Thanks


Sorry, but the documentation doesn't say that. Here's a reproduction of the full documentation for the function:

shutil.copyfile(src, dst)

Copy the contents (no metadata) of the file named src to a file named dst. dst must be the complete target file name; look at copy() for a copy that accepts a target directory path. If src and dst are the same files, Error is raised. The destination location must be writable; otherwise, an IOError exception will be raised. If dst already exists, it will be replaced. Special files such as character or block devices and pipes cannot be copied with this function. src and dst are path names given as strings.

So you have to create the directory yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜