Intro
I recently had to make a Python application parse some command-line arguments. No problem, sys.argv, right? Well, kind of. One of the arguments is a file name. Which is fine as long as it's pure ASCII. Or in the encoding Python expects. However, that's not always the case. I have a Hungarian Windows XP for running py2exe, and while testing I found that my app dies with a 'ASCII codec couldn't decode...' kind of error when there are non-ASCII characters passed in as arguments. Unfortunately that's exactly what happens when one opens a file associated to the application (and there are special characters in the name.)
Solution
The point is that we have to somehow turn the filename into something Python can handle. It's documented alright, but took me some time to find. The function we want:
sys.getfilesystemencoding()This, according to pydoc, returns the encoding used to convert Unicode filenames in operating system filenames. See Effbot for some more details.
No comments:
Post a Comment