Prev: how to build same executabl with andwithout console output
Next: Use cases for "setattr" in existing code
From: Jean-Michel Pichavant on 30 Jul 2010 12:28 Paul Lemelle wrote: > JM, > > Thanks for the response. > > I am trying to capture the stdout of a program from another program. > Example, I want to launch the below program from a second python > script then capture the first's program stdout to a file or variable. > > Is this possible? > > Thanks again, > Paul > use the subprocess module. import subprocess proc = subprocess.Popen(['echo', 'Hello World'], stdout=subprocess.PIPE, stderr=subprocess.PIPE ) out, err = proc.communicate() print out >> Hello World more details here http://docs.python.org/library/subprocess.html JM
From: Jean-Michel Pichavant on 2 Aug 2010 04:50
Paul Lemelle wrote: > Hi JM, > > My last dumb question: When I try to run the below script as an > executable, I get the following error: > pttdev(a)pttTestVM:~$ ./argv.py 4 > import: unable to open X server `/tmp/launch-c8feFG/org.x:0' @ > import.c/ImportImageCommand/361. > ./argv.py: line 8: syntax error near unexpected token `print' > ./argv.py: line 8: ` print x' > > The script: > # Writing the output to a standard argv argument > > #!/usr/bin/env python > > import sys > > for x in sys.argv: > print x > > > #END > > Any idea why? > > Thanks, > Paul > Please don't remove python-list. I see 2 possibilities: 1/ this is related to the open X server, for that I can't help you, something related to your exported display if you're remotely logged on the machine 2/ you are using python 2.x syntax within a python 3 interpreter. Try "print (x)". JM |