vendredi 17 avril 2015

Trying to delete folders with Unicode names


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)


I'm trying to use this code to delete empty folders, but it doesn't detect folders with characters like » and ▶...


I've tried enclosing all my path variables in unicode(), but that returns things like:



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.


I've done



reload(sys)
sys.setdefaultencoding("utf-8")


But that doesn't help either..,


...


Should I just switch to Python 3?


Aucun commentaire:

Enregistrer un commentaire