這只是刪除空目錄,並拉出目錄的單個文件。它似乎只回答問題的一部分,抱歉。
我在結尾加上一個循環來不斷嘗試,直到它再也找不到。我讓這個函數返回一個被刪除的目錄。
我拒絕訪問錯誤被固定的:shutil.rmtree fails on Windows with 'Access is denied'
import os
import shutil
def onerror(func, path, exc_info):
"""
Error handler for ``shutil.rmtree``.
If the error is due to an access error (read only file)
it attempts to add write permission and then retries.
If the error is for another reason it re-raises the error.
Usage : ``shutil.rmtree(path, ignore_errors=False, onerror=onerror)``
"""
import stat
if not os.access(path, os.W_OK):
# Is the error an access error ?
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
def get_empty_dirs(path):
# count of removed directories
count = 0
# traverse root directory, and list directories as dirs and files as files
for root, dirs, files in os.walk(path):
try:
# if a directory is empty there will be no sub-directories or files
if len(dirs) is 0 and len(files) is 0:
print u"deleting " + root
# os.rmdir(root)
shutil.rmtree(root, ignore_errors=False, onerror=onerror)
count += 1
# if a directory has one file lets pull it out.
elif len(dirs) is 0 and len(files) is 1:
print u"moving " + os.path.join(root, files[0]) + u" to " + os.path.dirname(root)
shutil.move(os.path.join(root, files[0]), os.path.dirname(root))
print u"deleting " + root
# os.rmdir(root)
shutil.rmtree(root, ignore_errors=False, onerror=onerror)
count += 1
except WindowsError, e:
# I'm getting access denied errors when removing directory.
print e
except shutil.Error, e:
# Path your moving to already exists
print e
return count
def get_all_empty_dirs(path):
# loop till break
total_count = 0
while True:
# count of removed directories
count = get_empty_dirs(path)
total_count += count
# if no removed directories you are done.
if count >= 1:
print u"retrying till count is 0, currently count is: %d" % count
else:
break
print u"Total directories removed: %d" % total_count
return total_count
count = get_all_empty_dirs(os.getcwdu()) # current directory
count += get_all_empty_dirs(u"o:\\downloads\\") # other directory
print u"Total of all directories removed: %d" % count
你嘗試過什麼?您使用什麼軟件包來瀏覽/瀏覽文件樹? – JonathanV
嗨。你看起來很新。如果您希望人們幫助您,我鼓勵您知道向我們展示您迄今爲止編寫的代碼,我們會盡力從此基礎爲您提供幫助。 – Ketouem
執行'os.walk'來獲取所有文件,執行'os.path.join'獲取完整的文件路徑進行處理。最終刪除根目錄(這將刪除它下面的所有內容) – inspectorG4dget