472,995 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,995 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 4819
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Darren Dale | last post by:
I'm a developer on the matplotlib project, and I am having trouble with the subprocess module on windows (Python 2.4.2 on winXP). No trouble to report with linux. I need to use _subprocess instead...
3
by: Ivan Vinogradov | last post by:
Dear All, I would greatly appreciate a nudge in the right direction concerning the use of cwd argument in the call function from subprocess module. The setup is as follows: driver.py <-...
1
by: yc | last post by:
I have a encoding problem during using of subprocess. The input is a string with UTF-8 encoding. the code is: tokenize =...
5
by: Johann C. Rocholl | last post by:
The following is my first attempt at adding a taint feature to Python to prevent os.system() from being called with untrusted input. What do you think of it? # taint.py - Emulate Perl's taint...
13
by: bayer.justin | last post by:
Hi, I am trying to communicate with a subprocess via the subprocess module. Consider the following example: <subprocess.Popen object at 0x729f0> Here hey is immediately print to stdout of...
2
by: smitty1e | last post by:
The first print statement does what you'd expect. The second print statement has rather a lot of rat in it. The goal here is to write a function that will return the man page for some command...
12
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's...
23
by: Harishankar | last post by:
Hi, Sorry to start off on a negative note in the list, but I feel that the Python subprocess module is sorely deficient because it lacks a mechanism to: 1. Create non-blocking pipes which can...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.