[Python] Coding and execfile

I am trying to do something like this with python 2.4:

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

afile = unicode('C:\\國立國語院.py', 'UTF-8')
execfile(afile.encode("UTF-8",'replace'))

And I get 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 make an execfile if the file I want to execute has a name with Korean characters?

Many thanks

+3
source share
2 answers

I think you should just do execfile(afile)with the 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))
+2
source

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

execfile()can be emulated with exec:

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

exec open(ur'C:\國立國語院.py').read()
+2
source

All Articles