相关疑难解决方法(0)

删除Python中的目录

shutil.rmtree不会删除Windows上的只读文件.有一个python相当于"rm -rf"?为什么哦为什么会这么痛?

python directory delete-file

31
推荐指数
1
解决办法
2万
查看次数

Python脚本未在Windows中删除Git文件

我正在使用以下代码删除包含git repo的目录:

import errno
import os
import stat
import shutil


def clear_dir(path):
    shutil.rmtree(path, ignore_errors=False, onerror=handle_remove_readonly)


def handle_remove_readonly(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
Run Code Online (Sandbox Code Playgroud)

此代码应能很好地处理只读文件。我可以从Windows资源管理器中删除目录/文件夹,但是当我运行以下代码时:

if __name__ == '__main__':
    clear_dir(r'c:\path\to\ci-monitor')
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

  File "C:\Users\m45914\code\ci-monitor\utils\filehandling.py", line 8, in clear_dir                              
    shutil.rmtree(path, ignore_errors=False, onerror=handle_remove_readonly)                                      
  File "C:\Users\m45914\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 488, in rmtree                
    return _rmtree_unsafe(path, onerror)                                                                          
  File "C:\Users\m45914\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 378, in _rmtree_unsafe        
    _rmtree_unsafe(fullname, onerror)                                                                             
  File "C:\Users\m45914\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 378, in _rmtree_unsafe        
    _rmtree_unsafe(fullname, …
Run Code Online (Sandbox Code Playgroud)

python windows git delete-file

5
推荐指数
1
解决办法
1600
查看次数

标签 统计

delete-file ×2

python ×2

directory ×1

git ×1

windows ×1