【发布时间】:2014-12-25 13:47:04
【问题描述】:
我正在尝试删除驱动器上的一组文件夹。这些目录不为空。我想出了一个解决方案如下:
import shutil
import os
path = "main/"
folderList = ['Blah', 'Blah', 'Blah'];
print ("Cleaning Project at %s" % path)
for c in folderList:
strippedPath = (path + c).strip("\n")
print ("Cleaning path " + strippedPath)
if os.path.exists(strippedPath):
try:
shutil.rmtree(strippedPath)
except OSError as why:
pass
print ("Done Cleaning Project")
问题是如果没有 try / catch 我会得到一个错误提示
PermissionError: [WinError 5] Access is denied: 'PathToFileHere'
在 Windows 上按删除键可以正常工作。有人可以给我一个命令来删除这个目录而不会出错吗?
【问题讨论】:
-
我在这里真的很明显,但这是因为 Python 没有您的操作系统的许可来完成该操作。这不是您可以在代码中解决的问题。虽然可能有类似
subprocess的解决方法。 -
是什么让这个文件夹如此特别?它会删除其余部分吗?
标签: python windows python-3.x