4

I am trying to do something like that using 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?

Thank you very much

jfs
  • 399,953
  • 195
  • 994
  • 1,670
Kvoste
  • 41
  • 2

2 Answers2

2

I think you should just be able to do execfile(afile) with a unicode argument on Windows, but I can't test it.

If not, get the file system encoding:

import sys
fsenc = sys.getfilesystemencoding()
execfile(afile.encode(fsenc))
Thomas K
  • 39,200
  • 7
  • 84
  • 86
  • Thanks for your help I do it like that #!/usr/bin/python # -*- coding: utf-8 -*- import sys, os afile = u'C:\\國立國語院.py' fsenc = sys.getfilesystemencoding() execfile(afile.encode(fsenc)) And i still have No such file or directory: 'C:\\?????.py – Kvoste May 02 '11 at 11:59
  • But what happens if you just give the unicode filename to `execfile` *without* encoding it first? You shouldn't need to encode the filename explicitly at all (although the specific Python version may affect that - this is an area where Python has improved in recent versions). – ncoghlan May 02 '11 at 12:27
  • If I just give the unicode filename I have this error : `UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-7` – Kvoste May 02 '11 at 12:36
  • #Kvoste I have test this answer on my PC ( win7 Chinese version ), and it runs without any problem. PS: My getfilesystemencoding() is "mbcs". – winterTTr May 02 '11 at 13:28
  • @Kvoste: OK, maybe execfile can't handle that unicode. Try @J.F. Sebastian's answer: open it, read it, and `exec` it. For some background, see this PEP: http://www.python.org/dev/peps/pep-0277/ – Thomas K May 02 '11 at 13:36
  • For me it's not working at all... Can it be a Windows XP problem and not a python one? – Kvoste May 02 '11 at 14:21
  • 3
    PEP 277 allows most file-handling interfaces to accept a Unicode string for access to Windows filenames outside the limitations of filesystemencoding. However `execfile` was not granted this feature, possibly because it was viewed as deprecated; it has been removed from Python since, with `exec open(...).read()` as the generally-foreseen replacement. – bobince May 02 '11 at 17:13
2

@Thomas K's answer should work (it works on Linux and doesn't work in Wine on Python2.4).

execfile() could be emulated using exec:

#!/usr/bin/python
# -*- coding: utf-8 -*-

exec open(ur'C:\國立國語院.py').read()
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • I have the same problem as I have with exevfile :( – Kvoste May 02 '11 at 15:30
  • 1
    Exact example/error? This works for me, as long as the actual string is definitely right, eg. `exec open(u'C:\\\u570b\u7acb\u570b\u8a9e\u9662.py')` to isolate source encoding problems. Obviously if the target `.py` file itself contains non-ASCII characters, it too will need the `coding` header comment. – bobince May 02 '11 at 17:16