据说Python 2.6.5支持Unicode?为什么listdir()不在IDLE中,但是Python 3.1.2确实在IDLE中显示了Unicode?(这是在Windows 7上测试的)
以下代码是相同的行为:
for dirname, dirnames, filenames in os.walk('c:\path\somewhere'):
for subdirname in dirnames:
print (os.path.join(dirname, subdirname))
for filename in filenames:
print (os.path.join(dirname, filename))
Run Code Online (Sandbox Code Playgroud)
更新: unicode在文件名中,而不在路径中...
Unicode字符串的语法从2更改为3.尝试指定Unicode字符串,如下所示:
u'c:\\path\\somewhere'
Run Code Online (Sandbox Code Playgroud)
如果您想要Python 3的语法(除非b给出前缀,否则字符串文字默认为Unicode ),请使用
from __future__ import unicode_literals
Run Code Online (Sandbox Code Playgroud)
在您的文件的顶部.