Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Opening a unicode named xls file from the script

How do you open a unicode named file (with spaces) from within a Python script under Windows?
filename for example: Hello עולם.xls

For a non-unicode non-spaced xls file, os.system(filename) works well.
For a non-unicode spaced xls file, os.system('"'+filename+'"') works well.

But for a unicode spaces xls file...

both os.system(filename) and subprocess.call(new_filename) give:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 12-13: ordinal not in range(128)

os.system(new_filename.encode('UTF-8')) gives:

'Hello' is not recognized as an internal or external command, operable program or batch file.

and subprocess.call(new_filename.encode('UTF-8')) gives:

WindowsError: [Error 2] The system cannot find the file specified

like image 804
Jonathan Livni Avatar asked Jun 14 '26 08:06

Jonathan Livni


2 Answers

os.startfile() as mentioned by Bradley (+1), but make sure to pass a Unicode string in, and not a byte string.

Windows NT filenames are natively Unicode, and Python on Windows has (unlike most other scripting languages) specific support built in for passing Unicode-strings into APIs that expect filenames:

os.startfile(u'Hello \u05e2\u05d5\u05dc\u05dd.xls')  # u'Hello עולם.xls'

If you pass in a byte string it will go instead to the standard C stdio library, which on the Microsoft C Runtime will map byte strings to Unicode filenames using the machine's default character set (aka ANSI code page), which is what getfilesystemencoding() is returning. That'll still work if every character in the filename is representable in the ANSI code page, but the example filename would fail for anything but a Hebrew installation of Windows.

Unfortunately the same Unicode support isn't available for system() or subprocess. But you probably don't need to use the command line in this case.

like image 57
bobince Avatar answered Jun 16 '26 02:06

bobince


You should be using os.startfile(), not os.system(). You probably also want to use sys.getfilesystemencoding() e.g.

import os
import sys
os.startfile(filename.encode(sys.getfilesystemencoding()))
like image 44
bradley.ayers Avatar answered Jun 16 '26 02:06

bradley.ayers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!