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

本文详细解析了在Windows环境下使用Python的shutil.rmtree()函数删除文件夹时出现PermissionError错误的原因,并提供了有效的解决方案,通过设置错误处理函数handle_remove_read_only,解决了文件或文件夹处于只读状态导致的删除失败问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值