noctufaber noctufaber - 1 month ago 7
Python Question

Unable to Exit or Quit Python Script From Command Line

I am running this python code from the command line:

# run on command line as: python firstscript.py
import sys, subprocess

pid = subprocess.Popen([sys.executable, 'secondscript.py']).pid
sys.exit()


Unfortunately I can't get it to exit all the way to the command line. If I hit the enter key (on OSX) it will finally exit. Is there a way to force the script to exit all the way to the command line without lingering in this weird limbo state? Also, I don't want to redirect stdout or stderr anywhere else because if I do, I lose the ability in secondscript.py to log output to a log file.

Thanks for the help.

Answer

The changes below worked for me:

# run on command line as: python firstscript.py    
import sys, subprocess 

process = subprocess.Popen([sys.executable, 'secondscript.py'])
output = process.communicate()[0]
Comments