#! /usr/bin/env python
import os
import sys
if len(sys.argv) < 2:
print 'Need to force directories into sys.argv'
#sys.argv += ["C:\Users\Andy\Desktop"]
#sys.argv += ["C:\Users\Andy\Desktop\Webpages"]
sys.argv += ["C:\Users\Andy\Desktop\Downloads (2)"]
def removeEmptyFolders(path):
if not os.path.isdir(path):
return
# remove empty subfolders
files = os.listdir(path)
if len(files):
for f in files:
fullpath = os.path.join(path, f)
if os.path.isdir(fullpath):
removeEmptyFolders(fullpath)
# if folder empty, delete it
files = os.listdir(path)
if len(files) == 0:
print "Removing empty folder:", path
os.rmdir(path)
for x in sys.argv[1:]:
print 'Scanning directory "%s"....' % x
removeEmptyFolders(x)
print 'Done.'
我想使用此代碼刪除空文件夾,但沒有檢查出像»和▶字符的文件夾...試圖用Unicode名稱刪除文件夾
我試着在封閉的unicode()我所有的路徑變量,但返回的東西,如:
Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 37, in <module>
File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 27, in removeEmptyFolders
File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 27, in removeEmptyFolders
File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 32, in removeEmptyFolders
File "C:\Program Files (x86)\Wing IDE 101 5.0\bin\2.7\src.zip\debug\tserver\dbgutils.py", line 1491, in write
UnicodeEncodeError: 'cp932' codec can't encode character u'\xbb' in position 54: illegal multibyte sequence
我做
reload(sys)
sys.setdefaultencoding("utf-8")
但這也沒有幫助。如果沒有的Unicode() - S,它只是讓我:
Need to force directories into sys.argv
Scanning directory "C:\Users\Andy\Desktop\Downloads (2)"....
Done.
Traceback (most recent call last):
File "C:\Users\Andy\Desktop\Delete Empty Folders.py", line 959, in <module>
AttributeError: 'file' object has no attribute '_FixGetPass'
而且都符合Unicode() - ...沒一樣不默認編碼變化。
注:我使用Wing IDE。
我應該只是切換到Python 3?
那麼你在'dbgutils'中有哪些代碼會觸發編碼錯誤?爲什麼涉及這個功能呢?使用unicode正是這個函數應該使用的。 –
@MartijnPieters - 我還沒有用dbgutils完成_anything_ ......我甚至不知道它是什麼......除了最可能的:「調試實用程序」... – Malandy
對,它是你的IDE的調試工具也許?你沒有在這裏包含完整的回溯,所以我甚至無法開始幫助調試該問題。 –