shutil.rmtree不适用于Windows库

时间:2014-05-29 01:05:35

标签: python windows file shutil

所以我正在构建一个简单的脚本,将某些文档备份到我的第二个硬盘驱动器(你永远不知道会发生什么!)。所以,我使用shutil.copytree函数在第二个驱动器上复制我的数据。它工作得很漂亮,这不是问题。

如果目标已存在,我使用shutil.rmtree函数删除树。我会告诉你我的代码:

import shutil
import os

def overwrite(src, dest):
    if(not os.path.exists(src)):
        print(src, "does not exist, so nothing may be copied.")
        return

    if(os.path.exists(dest)):
        shutil.rmtree(dest)

    shutil.copytree(src, dest)
    print(dest, "overwritten with data from", src)
    print("")

overwrite(r"C:\Users\Centurion\Dropbox\Documents", r"D:\Backup\Dropbox Documents")
overwrite(r"C:\Users\Centurion\Pictures", r"D:\Backup\All Pictures")

print("Press ENTER to continue...")
input()

正如您所看到的,一个简单的脚本。现在,当我第一次运行脚本时,一切都很好。图片和文档很好地复制到我的D:驱动器。但是,当我第二次跑步时,这是我的输出:

C:\Users\Centurion\Programming\Python>python cpdocsnpics.py
D:\Backup\Dropbox Documents overwritten with data from C:\Users\Centurion\Dropbox\Documents

Traceback (most recent call last):
  File "cpdocsnpics.py", line 17, in <module>
    overwrite(r"C:\Users\Centurion\Pictures", r"D:\Backup\All Pictures")
  File "cpdocsnpics.py", line 10, in overwrite
    shutil.rmtree(dest)
  File "C:\Python34\lib\shutil.py", line 477, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Python34\lib\shutil.py", line 376, in _rmtree_unsafe
    onerror(os.rmdir, path, sys.exc_info())
  File "C:\Python34\lib\shutil.py", line 374, in _rmtree_unsafe
    os.rmdir(path)
PermissionError: [WinError 5] Access is denied: 'D:\\Backup\\All Pictures'

只有在第一次复制Pictures后才会出现错误;我假设它与成为图书馆有关。

我该怎么办?

2 个答案:

答案 0 :(得分:4)

这是一个跨平台的一致性问题。 您已使用readonly属性复制了文件/目录。第一次&#34; dest&#34;不存在,因此不执行rmtree方法。但是,当你尝试运行&#34;覆盖&#34;功能我们可以注意到&#34; dest&#34; location存在(及其子树),但它是以只读访问方式复制的。所以我们遇到了一个问题。 为了修复&#34;问题,您必须为shutil.rmtree onerror 参数提供处理程序。只要你的问题与readonly问题有关,解决方法就像这样:

def readonly_handler(func, path, execinfo): 
    os.chmod(path, 128) #or os.chmod(path, stat.S_IWRITE) from "stat" module
    func(path)

正如您在python doc中看到的,onerror必须是一个可调用的,它接受三个参数:function,path和excinfo。有关详细信息,请read the docs.

def overwrite(src, dest):
    if(not os.path.exists(src)):
        print(src, "does not exist, so nothing may be copied.")
        return

    if(os.path.exists(dest)):  
        shutil.rmtree(dest, onerror=readonly_handler)

    shutil.copytree(src, dest)
    print(dest, "overwritten with data from", src)
    print("")

当然,这个处理程序很简单,但是如果发生其他错误,会引发新的异常并且这个处理程序可能无法修复它们!

注意: Tim Golden(Python for Windows贡献者)一直在修补shutil.rmtree问题,它似乎将在Python 3.5中得到解决(参见issue 19643)。

答案 1 :(得分:4)

我发现Windows上使用shutil.rmtree的只读文件以外的问题(在Windows 7上进行测试)。我正在使用shutil.rmtreeshutil.copytree的组合在测试套件中创建测试夹具,因此在短时间内(<1秒间隔)重复调用序列,并且我在测试套件中看到了不可预测的故障,报告了EACCES和ENOTEMPTY错误。这些症状告诉我,shutil.rmtree函数在返回调用程序时尚未完成,并且只是在一段时间后,删除的文件名才可以重复使用。

TL; DR:解决方案并不漂亮 - 广泛地说,它在删除目录之前重命名目录,但是由于Windows文件系统似乎需要一些时间来赶上,因此需要处理许多皱纹香水的运作。实际代码捕获各种故障条件,并在短暂延迟后重试失败操作的变体。

接下来是一个较长时间的讨论,我最后的代码将结束。

我的第一个想法是在删除之前尝试重命名目录树,以便原始目录名可以立即重用。这似乎有所帮助。为此,我创建了rmtree的替代品,其实质是:

def removetree(tgt):
    def error_handler(func, path, execinfo):
        e = execinfo[1]
        if e.errno == errno.ENOENT or not os.path.exists(path):
            return              # path does not exist - treat as success
        if func in (os.rmdir, os.remove) and e.errno == errno.EACCES:
            os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
            func(path)          # read-only file; make writable and retry
        raise e
    tmp = os.path.join(os.path.dirname(tgt),"_removetree_tmp")
    os.rename(tgt, tmp)
    shutil.rmtree(tmp, onerror=error_handler)
    return

我发现这个逻辑是一种改进,但它受到os.rename操作的不可预测的失败,其中一个可能的错误之一。所以我还在os.rename附近添加了一些重试逻辑,因此:

def removetree(tgt):
    def error_handler(func, path, execinfo):
        # figure out recovery based on error...
        e = execinfo[1]
        if e.errno == errno.ENOENT or not os.path.exists(path):
            return              # path does not exist
        if func in (os.rmdir, os.remove) and e.errno == errno.EACCES:
            os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
            func(path)          # read-only file; make writable and retry
        raise e
    # Rename target directory to temporary value, then remove it
    count = 0 
    while count < 10:           # prevents indefinite loop
        count += 1
        tmp = os.path.join(os.path.dirname(tgt),"_removetree_tmp_%d"%(count))
        try:
            os.rename(tgt, tmp)
            shutil.rmtree(tmp, onerror=error_handler)
            break
        except OSError as e:
            time.sleep(1)       # Give file system some time to catch up
            if e.errno in [errno.EACCES, errno.ENOTEMPTY]:
                continue        # Try another temp name
            if e.errno == errno.EEXIST:
                shutil.rmtree(tmp, ignore_errors=True)  # Try to clean up old files
                continue        # Try another temp name
            if e.errno == errno.ENOENT:
                break           # 'src' does not exist(?)
            raise               # Other error - propagate
    return

上面的代码没有经过测试,但这里的一般想法似乎确实有效。我实际使用的完整代码如下,并使用两个函数。它可能包含一些不必要的逻辑,但似乎对我来说更可靠(因为我的测试套件现在在Windows上反复传递,之前它在大多数运行中无法预测失败):

def renametree_temp(src):
    """
    Rename tree to temporary name, and return that name, or 
    None if the source directory does not exist.
    """
    count = 0 
    while count < 10:      # prevents indefinite loop
        count += 1
        tmp = os.path.join(os.path.dirname(src),"_removetree_tmp_%d"%(count))
        try:
            os.rename(src, tmp)
            return tmp      # Success!
        except OSError as e:
            time.sleep(1)
            if e.errno == errno.EACCES:
                log.warning("util.renametree_temp: %s EACCES, retrying"%tmp)
                continue    # Try another temp name
            if e.errno == errno.ENOTEMPTY:
                log.warning("util.renametree_temp: %s ENOTEMPTY, retrying"%tmp)
                continue    # Try another temp name
            if e.errno == errno.EEXIST:
                log.warning("util.renametree_temp: %s EEXIST, retrying"%tmp)
                shutil.rmtree(tmp, ignore_errors=True)  # Try to clean up old files
                continue    # Try another temp name
            if e.errno == errno.ENOENT:
                log.warning("util.renametree_temp: %s ENOENT, skipping"%tmp)
                break       # 'src' does not exist(?)
            raise           # Other error: propagaee
    return None

def removetree(tgt):
    """
    Work-around for python problem with shutils tree remove functions on Windows.
    See:
        https://stackoverflow.com/questions/23924223/
        https://stackoverflow.com/questions/1213706/
        https://stackoverflow.com/questions/1889597/
        http://bugs.python.org/issue19643
    """
    # shutil.rmtree error handler that attempts recovery from attempts 
    # on Windows to remove a read-only file or directory (see links above).
    def error_handler(func, path, execinfo):
        e = execinfo[1]
        if e.errno == errno.ENOENT or not os.path.exists(path):
            return          # path does not exist: nothing to do
        if func in (os.rmdir, os.remove) and e.errno == errno.EACCES:
            try:
                os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
            except Exception as che:
                log.warning("util.removetree: chmod failed: %s"%che)
            try:
                func(path)
            except Exception as rfe:
                log.warning("util.removetree: 'func' retry failed: %s"%rfe)
                if not os.path.exists(path):
                    return      # Gone, assume all is well
                raise
        if e.errno == errno.ENOTEMPTY:
            log.warning("util.removetree: Not empty: %s, %s"%(path, tgt))
            time.sleep(1)
            removetree(path)    # Retry complete removal
            return
        log.warning("util.removetree: rmtree path: %s, error: %s"%(path, repr(execinfo)))
        raise e
    # Try renaming to a new directory first, so that the tgt is immediately 
    # available for re-use.
    tmp = renametree_temp(tgt)
    if tmp:
        shutil.rmtree(tmp, onerror=error_handler)
    return

(上面的代码包含了来自What user do python scripts run as in windows?的只读文件问题的解决方案,根据Deleting directory in Python进行了测试。我不认为我遇到了只读文件问题,所以假设它没有在我的测试套件中测试过。)