10

我一直在尝试使用“copytree(src,dst)”,但是我不能,因为目标文件夹应该存在。在这里你可以看到我写的一小段代码:

def copy_dir(src,dest):
    import shutil
    shutil.copytree(src,dest)

copy_dir('C:/crap/chrome/','C:/test/') 

这是我得到的错误,正如我所料......

Traceback (most recent call last):
File "C:\Documents and Settings\Administrator\workspace\MMS-Auto\copy.py", line 5, in        <module>
copy_dir('C:/crap/chrome/','C:/test/')   
File "C:\Documents and Settings\Administrator\workspace\MMS-Auto\copy.py", line 3, in copy_dir
shutil.copytree(src,dest)
File "C:\Python27\lib\shutil.py", line 174, in copytree
os.makedirs(dst)
File "C:\Python27\lib\os.py", line 157, in makedirs
mkdir(name, mode)
WindowsError: [Error 183] Cannot create a file when that file already exists:    'C:/test/'

这是我的问题,有没有一种方法可以在不创建自己的 copytree 函数的情况下获得相同的结果?

先感谢您。

4

4 回答 4

14

我使用这个distutils包比这里的其他答案更成功。

http://docs.python.org/2/distutils/apiref.html#module-distutils.dir_util

distutils.dir_util.copy_tree函数的工作方式与它非常相似,shutil.copytree只是它dir_util.copy_tree只会覆盖存在的目录而不是因异常而崩溃。

代替:

import shutil
shutil.copytree(src, dst)

和:

import distutils.dir_util
distutils.dir_util.copy_tree(src, dst)
于 2013-06-28T05:53:42.260 回答
1

Look at errno for possible errors. You can use .copytree() first, and then when there is error, use shutil.copy.

From: http://docs.python.org/library/shutil.html#shutil.copytree

If exception(s) occur, an Error is raised with a list of reasons.

So then you can decide what to do with it and implement your code to handle it.

import shutil, errno

def copyFile(src, dst):
    try:
        shutil.copytree(src, dst)
    # Depend what you need here to catch the problem
    except OSError as exc: 
        # File already exist
        if exc.errno == errno.EEXIST:
            shutil.copy(src, dst)
        # The dirtory does not exist
        if exc.errno == errno.ENOENT:
            shutil.copy(src, dst)
        else:
            raise

About .copy(): http://docs.python.org/library/shutil.html#shutil.copy

Copy the file src to the file or directory dst. If dst is a directory, a file with the same basename as src is created (or overwritten) in the directory specified. Permission bits are copied. src and dst are path names given as strings.

Edit: Maybe also look into distutils.dir_util.copy_tree

于 2012-04-06T18:49:31.270 回答
1

在执行 shutil.copytree 函数之前,我需要先检查该位置是否存在同名目录。此外,它只复制带有某个通配符的目录。不确定是否需要回答这个问题,但我想把它留在那里。

import sys
import os
import os.path
import shutil 


src="/Volumes/VoigtKampff/Temp/_Jonatha/itmsp_source/"
dst="/Volumes/VoigtKampff/Temp/_Jonatha/itmsp_drop/"


for root, dirs, files in os.walk(src):
    for dir in dirs:
        if dir.endswith('folder'):
            print "directory to be moved: %s" % dir
            s = os.path.join(src, dir)
            d = os.path.join(dst, dir)
            if os.path.isdir(d):
                print "Not copying - because %s is already in %s" % (dir, dst)
            elif not os.path.isdir(d):
                shutil.copytree(s, d)
                print "Copying %s to %s" % (dir, dst)
于 2013-03-06T17:27:36.577 回答
0

我很确定这取决于你所拥有的 python 的确切版本,但是当我调用 shutil.copytree 时。医生我明白了:

使用 copy2() 递归地复制目录树。

目标目录必须不存在。

XXX 考虑这个示例代码而不是终极工具。

这解释了您遇到的错误。您可能可以改用distutils.dir_util.copy_tree

于 2012-04-06T18:45:17.467 回答