windows python删除文件文件夹报错:PermissionError

我遇到的问题

在windows中,使用shutil.rmtree(my_dir),会出现如下报错信息;在linux环境中,可以正常使用。

  File "F:\法克\k8s-package\package.py", line 124, in fetch_resource
    shutil.rmtree(RESOURCE_DIR)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\shutil.py", line 494, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\shutil.py", line 384, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\shutil.py", line 384, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\shutil.py", line 384, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\shutil.py", line 389, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\shutil.py", line 387, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 5] 拒绝访问。: 'F:\\法克\\k8s-package\\resources\\.git\\objects\\pack\\pack-0e0465dfd4ab2249348c43e4bd553c6aa4500b5d.idx'

问题分析

定位

根据原始报错信息,定位到最后一句os.unlink

os.unlink('F:\\法克\\k8s-package\\resources\\.git\\objects\\pack\\pack-0e0465dfd4ab2249348c43e4bd553c6aa4500b5d.idx')

Traceback (most recent call last):
  File "<input>", line 1, in <module>
PermissionError: [WinError 5] 拒绝访问。: 'F:\\法克\\k8s-package\\resources\\.git\\objects\\pack\\pack-0e0465dfd4ab2249348c43e4bd553c6aa4500b5d.idx'

查明原因

unlink使用详情。官网描述,unlink等同于remove

os.unlink(path, *, dir_fd=None)
Remove (delete) the file path. This function is semantically identical to remove(); the unlink name is its traditional Unix name.
Please see the documentation for remove() for further information.

Stack Overflow查找到,回答者是ThomasH, 内容如下:

We’ve had issues removing files and directories on Windows, even if we
had just copied them, if they were set to ‘readonly’. shutil.rmtree()
offers you sort of exception handlers to handle this situation. You
call it and provide an exception handler like this:

import errno, os, stat, shutil

def handleRemoveReadonly(func, path, exc):
  excvalue = exc[1]
  if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
      os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
      func(path)
  else:
      raise

shutil.rmtree(filename, ignore_errors=False, onerror=handleRemoveReadonly)

原因就是,如果是只读文件文件夹,shutil.rmtree()删除甚至复制,都会报权限错误

解决方案

ThomasH的是2009作答,可能是版本问题,以前使用的是os.remove,而当前版本使用的是unlink。于是,做了相应修改,添加了os.unlink

# 创建错误处理handle, 解决read only文件
def handle_remove_read_only(func, path, exc):
    excvalue = exc[1]
    if func in (os.rmdir, os.remove, os.unlink) and excvalue.errno == errno.EACCES:
      os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
      func(path)
    else:
      raise

if __name__ == '__main__':
    my_dir = 'resources'
    # 添加onerror
    shutil.rmtree(my_dir, onerror=handle_remove_read_only)
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐