[Python]编码与execfile
我正在尝试用 Python 2.4 做一些事情:
#!/usr/bin/python
# -*- coding: utf-8 -*-
afile = unicode('C:\\國立國語院.py', 'UTF-8')
execfile(afile.encode("UTF-8",'replace'))
但是我收到了这个错误:
IOError: [Errno 2] No such file or directory: 'C:\\\xef\xbb\xbf\xe5\x9c\x8b\xe7\xab\x8b\xe5\x9c\x8b\xe8\xaa\x9e\xe9\x99\xa2.py'
所以我的问题是,如果我想执行的文件名包含韩文字符,我该怎么做 execfile 呢?
非常感谢!
2 个回答
2
我觉得你应该可以在Windows上用一个unicode参数直接执行 execfile(afile)
,不过我没法测试这个。
如果不行的话,可以先获取文件系统的编码方式:
import sys
fsenc = sys.getfilesystemencoding()
execfile(afile.encode(fsenc))
2
@Thomas K的回答应该是有效的(在Linux上可以用,但在Python2.4的Wine环境下不行)。
execfile()
这个函数可以用exec
来模拟实现:
#!/usr/bin/python
# -*- coding: utf-8 -*-
exec open(ur'C:\國立國語院.py').read()