[Tutor] not invoking the shell from python

Hugo González Monteverde hugonz-lists at h-lab.net
Wed Jun 22 06:35:08 CEST 2005

Hi Wang,

You need to do what the shell does, all up the program directly, like this:

fork parent,
in the parent, reap the child
in the child, call exec() with the subprogram as the argument

Here's an example:

import os

program_executable = "/bin/ls"
parameters = ["/home/me/file1.txt", "/home/me/file2.txt"]

pid = os.fork()

if pid==0: #I'm the child
	os.execv(program_executable, program_executable + parameters)
else: #I'm the parent
	os.waitpid(-1) #wait and reap a child process

I have not been able to test it since I'm in Windows but this should do. 
This is basically just an implementation of what system() does... the 
waitpid() step is necessary in order not to leave a zombie process lying 

Hope it helps,


Shuying Wang wrote:
> Hi tutors,
> I've got an external program that I'm calling from python with
> os.popen. The problem is that I need to pass this program an arbitrary
> body of text. I've tried escaping characters before passing it as
> input but the shell still expands out certain characters. I noticed
> with python2.4. How do I bypass the shell and hand arguments directly
> to the program?
> The problematic line in question looks like this:
> os.popen('''rt create -t ticket set requestor='%s' owner='%s'
> queue='%s' subject="%s" text="%s" status='%s' ''' %
> 		  (data['requestor'],data['owner'],data['queue'],
> re.sub(r'\"',r'\\"',data['subject']), re.sub(r'\"',r'\\"',
> data['body']), data['status'])
> thanks in advance,
> Shuying
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list