473,569 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\u 672c\u8a9e_exec .bat', u'arg1', u'arg2' ]
subprocess.call (cmd)

But this gives the error:

UnicodeEncodeEr ror: '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 4853
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\u 672c\u8a9e_exec .bat', u'arg1', u'arg2' ]
subprocess.call (cmd)

But this gives the error:

UnicodeEncodeEr ror: '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.getfilesyst emencoding() to obtain the required encoding (likely
"mbcs"):

fse = sys.getfilesyst emencoding()
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.a r>
wrote:
En Fri, 07 Sep 2007 18:46:26 -0300, Matthew Lausch <mclau...@gmail .com>
escribi?:
I'd like to use thesubprocessmo dule with upper level characters in
the process name or in the arguments to the process. Something like
this:
cmd = [ u'test_\u65e5\u 672c\u8a9e_exec .bat', u'arg1', u'arg2' ]
subprocess.call (cmd)
But this gives the error:
UnicodeEncodeEr ror: '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.getfilesyst emencoding() to obtain the required encoding (likely
"mbcs"):

fse = sys.getfilesyst emencoding()
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
5495
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 of pywin32, but my trouble exists with either option: import subprocess process = subprocess.Popen(, stderr=subprocess.STDOUT,...
3
3584
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 <- python script core/ <- directory main <- fortran executable in the core directory
1
5171
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 = subprocess.Popen(tok_command,stdin=subprocess.PIPE,stdout=subprocess.PIPE,close_fds=True,shell=True) (tokenized_text,errs) = tokenize.communicate(t)
5
2145
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 feature in Python # Copyright (C) 2007 Johann C. Rocholl <johann@rocholl.net> # # Permission is hereby granted, free of charge, to any person #...
13
5094
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 my interpreter, I did not type in the "hey". But I want to read from the output into a string, so I do
2
1843
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 (mktemp used as a short example here) as text to client code, where the groff markup will be chopped to extract all of the command options. Those...
12
4515
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 finished, and if it is look at its output. I may want to send a signal at some point to kill the process. This seems straightforward, but it...
23
2042
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 be read in a separate thread (I am currently writing a mencoder GUI in Tkinter and need a full fledged process handler to control the command line...
7
6214
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 this shell script using the Subprocess module. Here is my code: def resultFromRunning_(command):
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7681
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.