I want my Python script to be able to read Unicode command line arguments in Windows. But it appears that sys.argv is a string encoded in some local encoding, rather than Unicode. How can I read the command line in full Unicode?
Example code: argv.py
import sys
first_arg = sys.argv[1]
print first_arg
print type(first_arg)
print first_arg.encode("hex")
print open(first_arg)
On my PC set up for Japanese code page, I get:
C:\temp>argv.py "PC・ソフト申請書08.09.24.doc"
PC・ソフト申請書08.09.24.doc
<type 'str'>
50438145835c83748367905c90bf8f9130382e30392e32342e646f63
<open file 'PC・ソフト申請書08.09.24.doc', mode 'r' at 0x00917D90>
That's Shift-JIS encoded I believe, and it "works" for that filename. But it breaks for filenames with characters that aren't in the Shift-JIS character set—the final "open" call fails:
C:\temp>argv.py Jörgen.txt
Jorgen.txt
<type 'str'>
4a6f7267656e2e747874
Traceback (most recent call last):
File "C:\temp\argv.py", line 7,
in <module>
print open(first_arg)
IOError: [Errno 2] No such file or directory: 'Jorgen.txt'
Note—I'm talking about Python 2.x, not Python 3.0. I've found that Python 3.0 gives sys.argv
as proper Unicode. But it's a bit early yet to transition to Python 3.0 (due to lack of 3rd party library support).
Update:
A few answers have said I should decode according to whatever the sys.argv
is encoded in. The problem with that is that it's not full Unicode, so some characters are not representable.
Here's the use case that gives me grief: I have enabled drag-and-drop of files onto .py files in Windows Explorer. I have file names with all sorts of characters, including some not in the system default code page. My Python script doesn't get the right Unicode filenames passed to it via sys.argv in all cases, when the characters aren't representable in the current code page encoding.
There is certainly some Windows API to read the command line with full Unicode (and Python 3.0 does it). I assume the Python 2.x interpreter is not using it.
Dealing with encodings is very confusing.
I believe if your inputing data via the commandline it will encode the data as whatever your system encoding is and is not unicode. (Even copy/paste should do this)
So it should be correct to decode into unicode using the system encoding:
running the following Will output: Prompt> python myargv.py "PC・ソフト申請書08.09.24.txt"
Where the "PC・ソフト申請書08.09.24.txt" contained the text, "日本語". (I encoded the file as utf8 using windows notepad, I'm a little stumped as to why there's a '?' in the begining when printing. Something to do with how notepad saves utf8?)
The strings 'decode' method or the unicode() builtin can be used to convert an encoding into unicode.
Also, if your dealing with encoded files you may want to use the codecs.open() function in place of the built-in open(). It allows you to define the encoding of the file, and will then use the given encoding to transparently decode the content to unicode.
So when you call
content = codecs.open("myfile.txt", "r", "utf8").read()
content
will be in unicode.codecs.open: http://docs.python.org/library/codecs.html?#codecs.open
If I'm miss-understanding something please let me know.
If you haven't already I recommend reading Joel's article on unicode and encoding: http://www.joelonsoftware.com/articles/Unicode.html
The command line might be in Windows encoding. Try decoding the arguments into
unicode
objects:Here is a solution that is just what I'm looking for, making a call to the Windows
GetCommandLineArgvW
function:Get sys.argv with Unicode characters under Windows (from ActiveState)
But I've made several changes, to simplify its usage and better handle certain uses. Here is what I use:
win32_unicode_argv.py
Now, the way I use it is simply to do:
and from then on,
sys.argv
is a list of Unicode strings. The Pythonoptparse
module seems happy to parse it, which is great.Try this:
Maybe you have to substitute
CP437
orCP1252
forUTF-8
. You should be able to infer the proper encoding name from the registry keyHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage\OEMCP