备份文件脚本

发布于 2024-10-31 11:25:44 字数 1391 浏览 1 评论 0原文

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

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

我已阅读文档并发现 Shutil.copyfile 将创建一个目录,如果 dir 不存在。但是我收到一条 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)

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

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,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)

Can anyone tell me where did I do wrong?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昨迟人 2024-11-07 11:25:44

抱歉,但文档没有这么说。以下是该函数的完整文档的副本:

shutil.copyfile(src, dst)

复制
文件的内容(无元数据)
将名为 src 的文件复制到名为 dst 的文件中。 夏令时
必须是完整的目标文件名;
查看 copy() 来获取接受的副本
目标目录路径。如果src并且
dst 是相同的文件,Error
提出。目的地位置必须
可写;否则,IOError
将引发异常。如果dst
已经存在,它将被替换。
特殊文件,例如字符或
块设备和管道不能
用这个功能复制。 srcdst
是以字符串形式给出的路径名。

所以你必须自己创建该目录。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文