Subprocess module and unicode input
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Fri Sep 7 22:42:43 EDT 2007
En Fri, 07 Sep 2007 18:46:26 -0300, Matthew Lausch <mclausch at gmail.com>
escribi�:
> I'd like to use the subprocess module with upper level characters in
> the process name or in the arguments to the process. Something like
> this:
>
> cmd = [ u'test_\u65e5\u672c\u8a9e_exec.bat', u'arg1', u'arg2' ]
> subprocess.call(cmd)
>
> But this gives the error:
>
> UnicodeEncodeError: 'ascii' codec can't encode characters in position
> 5-7: ordinal not in range(128)
>
> Is there a way around this problem? I don't want to assume any
> particular set of characters. The example above uses Japanese
> characters, but I would like to support anything.
You have to encode those unicode objects anyway - but you don't have to
assume any encoding.
Your .bat file will be executed by CMD.EXE, and the shell expects a string
- doesn't understand unicode objects. Like when you type that same
commands on the console.
Use sys.getfilesystemencoding() to obtain the required encoding (likely
"mbcs"):
fse = sys.getfilesystemencoding()
cmd = [arg.encode(fse) if isinstance(arg,unicode) else arg
for arg in cmd]
--
Gabriel Genellina
More information about the Python-list
mailing list