471,336 Members | 1,272 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

Subprocess module and unicode input

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.

Thanks.

Sep 7 '07 #1
2 4747
En Fri, 07 Sep 2007 18:46:26 -0300, Matthew Lausch <mc******@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

Sep 8 '07 #2
Thanks! That worked perfectly.
Matt

On Sep 7, 10:42 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Fri, 07 Sep 2007 18:46:26 -0300, Matthew Lausch <mclau...@gmail.com>
escribi?:
I'd like to use thesubprocessmodule 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

Sep 13 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Darren Dale | last post: by
3 posts views Thread by Ivan Vinogradov | last post: by
1 post views Thread by yc | last post: by
5 posts views Thread by Johann C. Rocholl | last post: by
12 posts views Thread by bhunter | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.