0

Guyz - 我有这个问题,shutil.copy 树认为一个目录存在,即使它不存在..源和目录都是本地的......我第一次运行它运行时没有错误,但内容实际上没有被复制,第二次它运行它认为该目录已经存在,详细信息如下..请提供您的输入,如果除了shutil之外还有其他复制方式..请建议

第一次运行,没有任何错误,但实际上并未复制

  <username:/local/mnt/workspace/username/Scripts>python test.py
    //local/mnt/workspace/loc/04.01.01.00.303_HY11/out
    //local/mnt/workspace/test/out
    copying

第二次重新运行,它认为它认为目录存在

    <username:/local/mnt/workspace/username/Scripts>python test.py
    //local/mnt/workspace/loc/04.01.01.00.303_HY11/out
    //local/mnt/workspace/test/out
    copying
    Traceback (most recent call last):
      File "test.py", line 21, in <module>
        main()
      File "test.py", line 18, in main
        copytree(src,dst)
      File "test.py", line 11, in copytree
        shutil.copytree(s, d)
      File "/pkg/qct/software/python/2.5.2/.amd64_linux26/lib/python2.5/shutil.py", line 110, in copytree
        os.makedirs(dst)
      File "/pkg/qct/software/python/2.5.2/.amd64_linux26/lib/python2.5/os.py", line 171, in makedirs
        mkdir(name, mode)
    OSError: [Errno 17] File exists: '//local/mnt/workspace/test/out'
    <username:/local/mnt/workspace/username/Scripts>

Python代码

import os,shutil

def copytree(src, dst, symlinks=False, ignore=None):
    for item in os.listdir(src):
        s = os.path.join(src, item)
        print s
        d = os.path.join(dst, item)
        print d
        if os.path.isdir(s):
            print "copying"
            shutil.copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)
def main ():
    src="//local/mnt/workspace/loc/04.01.01.00.303_HY11"
    dst="//local/mnt/workspace/test"
    copytree(src,dst)

if __name__ == '__main__':
    main()
4

2 回答 2

1

试试这个版本,目标目录会自动清空...

import os,shutil,errno

def copytree(src, dst, symlinks=False, ignore=None):
    if os.path.exists(dst):
        shutil.rmtree(dst)

    os.mkdir(dst)

    for item in os.listdir(src):
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        print s + " >> " + d

        if ".git" in s:
            return

        if os.path.isdir(s):
            print "Copying directory..."

            try:
                copytree(s, d, symlinks, ignore)

            except OSError as e: 
                # File already exist
                if e.errno == errno.EEXIST:
                    print "Path exists : " + d
        else:
            shutil.copy2(s, d)

def main ():
    src="//local/mnt/workspace/loc/04.01.01.00.303_HY11"
    dst="//local/mnt/workspace/test"
    copytree(src,dst)

if __name__ == '__main__':
    main()
于 2013-01-06T06:13:22.853 回答
0

请问为什么不用shutil.copytree()代替呢?如果您确实想要一个包装器shutil.copytree()(例如考虑现有目录),请将您的函数命名为不同的名称,例如copytree_wrapper()(因为您正在混合您的copytreewith shutil.copytree,并且递归不涉及您的copytree)以下对我有用:

import os,shutil

def copytree_wrapper(src, dst, symlinks=False, ignore=None): ### Name it different, no confusion!
    for item in os.listdir(src):
        s = os.path.join(src, item)
        print (s)
        d = os.path.join(dst, item)
        print (d)
        if os.path.isdir(s):
            print ("copying")
            if not os.path.exists(d): ### Create directory if does not already exist 
                print ("directory '%s' created" % d)
                os.makedirs(d)
            copytree_wrapper(s, d, symlinks, ignore) ### shutil.copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)
def main ():
    src="/tmp/a"
    dst="/tmp/b"
    copytree_wrapper(src,dst)

if __name__ == '__main__':
    main()
于 2013-01-06T06:14:31.400 回答