2

i would like to start a python file (.py) with arguments and receive the output of it after it is finished. i have already heard about "popen" and "subprocess.call" but i could not find any tutorials how to use them

does anyone know a good tutorial?

SilentGhost
322k67 gold badges312 silver badges294 bronze badges
asked Jul 23, 2010 at 9:33
1
  • 1
    subprocess and popen are used to call external programs from python, if you want to call python from python there are easier ways. Commented Jul 23, 2010 at 9:38

4 Answers 4

6

You don't need them ; just launch your file as a program giving argument like

./main.py arg1 arg2 arg3 >some_file

(for that your file must begin with something like #!/usr/bin/env python)

Using sys module you can access them :

arg1 = sys.argv[1]
arg2 = sys.argv[2]
arg3 = sys.argv[3]
S.Lott
393k83 gold badges521 silver badges791 bronze badges
answered Jul 23, 2010 at 9:39
Sign up to request clarification or add additional context in comments.

2 Comments

Misses though the part: receive the output of it after it is finished
> some_file pipes the output into a file, which certainly receives the output. If the OP wants the output on stdout as well, just replace > with tee
5

i would like to start a python file (.py) with arguments and receive the output of it after it is finished.

Step 1. Don't use subprocess. You're doing it wrong.

Step 2. Read the Python file you want to run. Let's call it runme.py.

Step 3. Read it again. If it's competently written, there is a block of code that starts if __name__ == "__main__":. What follows is the "external interface" to that file. Since you provided no information in the question, I'll assume it looks like this.

if __name__ == "__main__":
 main()

Step 4. Read the "main" function invoked by the calling script. Since you provided no information, I'll assume it looks like this.

def main():
 options, args = parse_options()
 for name in args:
 process( options, file )

Keep reading to be sure you see how parse_options and process work. I'll assume parse_options uses optparse.

Step 5. Write your "calling" script.

 import runme
 import sys
 import optparse
 options = options= optparse.Values({'this':'that','option':'arg','flag':True})
 with open( "theoutput.out", "w" ) as results:
 sys.stdout= results
 for name in ('some', 'list', 'of', 'arguments' ):
 runme.process( options, name )

This is the correct way to run a Python file from within Python.

Actually figure out the interface for the thing you want to run. And run it.

answered Jul 23, 2010 at 10:09

3 Comments

I would think that the run is with one type of parameters only. In In that case I would put input('Stopped') in if branches of main code or function called for processing to make sure which branch takes that form of parameters. Then I wuuld put print to print out the arguments of the called function with actual parameters I want to use. After that I would put to calling file import the function called as : from otherfile import activity activity(printed_out_parameters....)
@Tony Veijalainen: Very clever. I prefer to read the code and understand it. But empiricism can work with software.
your code doesn't work and is confusing. It would help me a lot if you'd explain what runme's process is because I tried setting up your code and it fails. It looks quite clever what you did but there's no way for me to really understand what you did without seeing it in action.
1

runme.py

print 'catch me'

main.py

import sys
from StringIO import StringIO
new_out = StringIO()
old_out = sys.stdout
sys.stdout = new_out
import runme
sys.stdout = old_out
new_out.seek(0)
print new_out.read()

and...

$ python main.py 
catch me
answered Jul 23, 2010 at 11:06

Comments

0

Unless you mean you want to start the Python file from within Python? In which case it's even nicer:

import nameOfPythonFile
answered Jul 23, 2010 at 9:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.