python unicode 中文输出 求助
# -*- coding:utf-8 -*-
import os
import urllib
def printfile(dir):
files = [f for f in os.listdir(dir)
if os.path.isfile(os.path.join(dir , f))]
for file in files:
name = unicode(file)
print name.encode( 'utf-8 ')
dire = [d for d in os.listdir(dir)
if os.path.isdir(os.path.join(dir , d))]
for x in dire:
#y = dir + "\\ " + x
printfile(os.path.join(dir, x))
printfile( 'e:/book ')
会输出的一个文件 然后报错
PerlCookBook.chm
The_Perl_Programming_Language_en.pdf
Traceback (most recent call last):
File "d:\MyProject\python\MyPython\listDir.py ", line 19, in <module>
printfile( 'e:/book ')
File "d:\MyProject\python\MyPython\listDir.py ", line 10, in printfile
name = unicode(file)
UnicodeDecodeError: 'ascii ' codec can 't decode byte 0xa9 in position 3: ordinal not in range(128)
请问 应该怎么写才会不出错呢
[解决办法]
name = unicode(file, "cp936 ")