开发者

Python: Opening a unicode named xls file from the script

开发者 https://www.devze.com 2023-02-22 02:47 出处:网络
How do you open a unicode named file (with spaces) from within a Python script under Windows? filename for example: Hello עולם.xls

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


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.


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()))
0

精彩评论

暂无评论...
验证码 换一张
取 消