[英][Python]Encoding and execfile
I am trying to do something like that using python 2.4:我正在尝试使用 python 2.4 做类似的事情:
#!/usr/bin/python
# -*- coding: utf-8 -*-
afile = unicode('C:\\國立國語院.py', 'UTF-8')
execfile(afile.encode("UTF-8",'replace'))
And I receive this error:我收到这个错误:
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'
So my question is how can I do an execfile if the file I want to exec have a name with Korean chars?所以我的问题是,如果我要执行的文件的名称带有韩语字符,我该如何执行 execfile?
Thank you very much非常感谢
I think you should just be able to do execfile(afile)
with a unicode argument on Windows, but I can't test it.我认为您应该能够在 Windows 上使用 unicode 参数执行execfile(afile)
,但我无法对其进行测试。
If not, get the file system encoding:如果没有,请获取文件系统编码:
import sys
fsenc = sys.getfilesystemencoding()
execfile(afile.encode(fsenc))
@Thomas K's answer should work (it works on Linux and doesn't work in Wine on Python2.4). @Thomas K 的答案应该有效(它适用于 Linux 并且不适用于 Python2.4 上的 Wine)。
execfile()
could be emulated using exec
: execfile()
可以使用exec
来模拟:
#!/usr/bin/python
# -*- coding: utf-8 -*-
exec open(ur'C:\國立國語院.py').read()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.