Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get output of python script from within python script

Tags:

People also ask

How do you call a Python program from another Python program?

Use the execfile() Method to Run a Python Script in Another Python Script. The execfile() function executes the desired file in the interpreter. This function only works in Python 2. In Python 3, the execfile() function was removed, but the same thing can be achieved in Python 3 using the exec() method.

How do I get output in Python?

In Python 3. x, you can output without a newline by passing end="" to the print function or by using the method write: import sys print("Hello", end="") sys. stdout.


printbob.py:

import sys
for arg in sys.argv:
    print arg

getbob.py

import subprocess
#printbob.py will always be in root of getbob.py
#a sample of sending commands to printbob.py is:
#printboby.py arg1 arg2 arg3   (commands are seperated by spaces)

print subprocess.Popen(['printbob.py',  'arg1 arg2 arg3 arg4']).wait()

x = raw_input('done')

I get:

  File "C:\Python27\lib\subprocess.py", line 672, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 882, in _execute_child
    startupinfo)
WindowsError: [Error 193] %1 is not a valid Win32 application

What am I doing wrong here? I just want to get the output of another python script inside of another python script. Do I need to call cmd.exe or can I just run printbob.py and send commands to it?