Passing data from python to an external command

I read everything that is on subprocess.Popen, but I think something is missing.

I need to be able to execute a unix program that reads a data stream from a list created in a python script and writes the result of this program to a file. From the bash prompt, I do this all the time without problems, but now I'm trying to do this from within a python script that preprocesses some binary files and a lot of data before proceeding with this step.

Let's look at a simple example that does not include all the preprocessing:

import sys from pylab import * from subprocess import * from shlex import split # some arbitrary x,y points points = [(11,31),(13,33),(15,37),(16,35),(17,38),(18,39.55)] commandline = 'my_unix_prog option1 option2 .... > outfile' command = split(commandline) process = Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE) print process.communicate(str(points)) 

The way this will run in bash is:

 echo "11 31 13 33 15 37 16 35 17 38 18 39.55" | my_unix_prog option1 option2 .... > outfile 

The way data is passed to the unix program is also important, I have to format it in 2 columns, separated by spaces.

Any help is appreciated ...

+4
source share
3 answers

SOLVE!

With the help of Dhara and xhainingx, I was able to figure this out:

 import sys from pylab import * from subprocess import * from shlex import split # some arbitrary x,y points points = [(11,31),(13,33),(15,37),(16,35),(17,38),(18,39.55)] commandline = 'my_unix_prog option1 option2 ....' command = split(commandline) process = Popen(command, stdin=PIPE, stdout=open('outfile', 'w'), stderr=PIPE) for p in points: process.stdin.write(str(p[0]) + ' ' + str(p[1]) + '\n') print process.communicate() 

This works very well, thanks.

+3
source

what about something like

 for p in points: process.stdin.write(str(p[0]) + ' ' + str(p[1]) + '\n') print process.communicate() 
+1
source

You need to format the input for the correct connection.

str will save special characters when printing a list of tuples, which is not what you want.

 >>> print str([(1,2), (3,4)]) [(1,2), (3,4)] 

Try the following:

 print process.communicate("\n".join(["%s %s"%(x[0], x[1]) for x in points]) 
0
source

All Articles