【发布时间】:2011-03-05 21:35:43
【问题描述】:
我刚刚在我的系统上设置了 PortablePython,所以我可以从 PHP 运行 python 脚本,并且我得到了一些非常基本的代码(如下)来列出目录中的所有文件,但是它不适用于日文文件名。它适用于英文文件名,但是当我将任何包含日文字符的文件放在目录中时,它会吐出错误(如下)。
import os, glob
path = 'G:\path'
for infile in glob.glob( os.path.join(path, '*') ):
print("current file is: ", infile)
使用 'PyScripter-Portable.exe' 可以正常工作,但是当我尝试在命令提示符或 PHP 中运行 'PortablePython\App\python.exe "test.py"' 时,会出现以下错误:
current file is: Traceback (most recent call last):
File "test.py", line 5, in <module>
print("current file is: ", infile)
File "PortablePython\App\lib\io.py", line 1494, in write
b = encoder.encode(s)
File "PortablePython\App\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 37-40: character maps to <undefined>
我对 Python 很陌生,只是用它来解决 PHP 问题,无法在 Windows 中读取 unicode 文件名......所以我真的需要这个工作 - 你能给我的任何帮助都会很棒。
【问题讨论】:
-
请阅读您的回溯并注意
glob有效,但print无效!
标签: python windows unicode directory