python os.popen fails when given umlauted character
Under winxp (pro x64 version 2003, sp2), running the following at cmd prompt works fine:
dir C:\testüüüdirectory\
Please note the 'ü' character.
But from python 2.7.1.4, the following always retur开发者_StackOverflow社区ns an error code 1:
>>> res = os.popen("dir C:\\testüüüdirectory\\")
>>> res.close()
1
>>>
How can i get popen to accept the ü character?
This would probably be an encoding issue, where your input doesn't quite match what the file system expects on output. First, you'll need to tell Python what encoding your input uses, then what encoding the popen()
call expects.
>>> filename = "C:\\testüüüdirectory\\".decode("utf-8")
>>> res = os.popen("dir " + filename.encode("cp1252"))
You may have to experiment with various encoding combinations to get the right pair. UTF-8, UTF-16, and cp1252 are the most likely candidates for Windows, but your system might be set up to use another encoding. If you're really lucky, os.popen()
might even accept a Unicode string, in which case the encode()
step could be skipped.
If you're really unlucky, character canonicalization could be an issue. There are two possible representations of "ü" in Unicode: U+00FC or U+0075 U+0308; the second is a "u" with a combining diaresis, while the first is pre-combined. If Windows expects one while encode()
produces the other, you may have to work a bit to get the right string.
Try it with a u
in front of the string:
res = os.popen(u"dir C:\\testüüüdirectory\\")
More on unicode strings / lexical analysis
Check sys.getfilesystemencoding also to make sure you can have such file names.
精彩评论