Subprocess module and unicode input

Matthew Lausch mclausch at gmail.com
Thu Sep 13 17:47:26 EDT 2007


Thanks! That worked perfectly.
Matt

On Sep 7, 10:42 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Fri, 07 Sep 2007 18:46:26 -0300, Matthew Lausch <mclau... at 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





More information about the Python-list mailing list