备份文件脚本

2024-09-29 21:21:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个脚本来备份文件从一个目录(主目录)到另一个目录(克隆)。 脚本将监视这两个目录。在

如果克隆内的文件丢失,则脚本将把丢失的文件从主文件复制到 克隆。现在创建丢失的文件夹时遇到问题。在

我已经阅读了文档并发现shutil.copyfile文件如果 迪尔没有存在。但是我收到一条IOError消息显示目标目录 不是存在。下面是密码。在

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)

谁能告诉我我哪里做错了? 谢谢


Tags: 文件pathinmasterclientforiflocation
1条回答
网友
1楼 · 发布于 2024-09-29 21:21:06

抱歉,文件上没有这么说。以下是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.

你必须自己创建目录。在

相关问题 更多 >

    热门问题