尝试删除具有 Unicode 名称的文件夹
Trying to delete folders with Unicode names
#! /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() 中,但是 returns 类似:
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.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()-s... 与不更改默认编码相同。
注意:我使用的是 Wing IDE。
我应该切换到 Python 3 吗?
路径使用unicode
是正确的解决方案;如果您将 unicode
值传递给 os.listdir()
,它将生成 unicode
个文件名,并且一切正常。
你的回溯实际上是由你的print
语句引起的:
print "Removing empty folder:", path
Which WingIDE 尝试编码以供网络使用,但失败了,因为您的系统编码(代码页 932)无法对路径中的某些字符进行编码。
您可以使用 repr()
解决该部分问题:
print "Removing empty folder:", repr(path)
因为这至少为您提供了一个可调试的路径表示,其中任何不可打印的非 ASCII 字符都替换为转义码。
#! /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() 中,但是 returns 类似:
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.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()-s... 与不更改默认编码相同。
注意:我使用的是 Wing IDE。
我应该切换到 Python 3 吗?
路径使用unicode
是正确的解决方案;如果您将 unicode
值传递给 os.listdir()
,它将生成 unicode
个文件名,并且一切正常。
你的回溯实际上是由你的print
语句引起的:
print "Removing empty folder:", path
Which WingIDE 尝试编码以供网络使用,但失败了,因为您的系统编码(代码页 932)无法对路径中的某些字符进行编码。
您可以使用 repr()
解决该部分问题:
print "Removing empty folder:", repr(path)
因为这至少为您提供了一个可调试的路径表示,其中任何不可打印的非 ASCII 字符都替换为转义码。