python unicode 中文输出 求助

python unicode 中文输出 求助



[Copy to clipboard] [ - ]
CODE:
# -*- 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')



[Copy to clipboard] [ - ]
CODE:
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)

看错误信息应该是没有按照utf-8来处理  为什么呢 呵呵
好吧  我在csdn上问到了 呵呵
name = unicode(file.'cp936')

初学期间 低级问题可能比较多 大家请见谅
不错, 转载了 http://www.cnscn.org


QUOTE:
原帖由 DraculaW 于 2007-4-24 13:59 发表
好吧  我在csdn上问到了 呵呵
name = unicode(file.'cp936')

初学期间 低级问题可能比较多 大家请见谅


name = unicode(file.'gb2312')
行吗?