noctufaber
noctufaber

Reputation: 794

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.

Upvotes: 1

Views: 1072

Answers (2)

noctufaber
noctufaber

Reputation: 794

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]

Upvotes: 1

kbcool
kbcool

Reputation: 735

You seem to be asking if there is a better way to do this. Check out check_output. I have always found it much more convenient and fool proof compared to the lower level stuff in subprocess.

Upvotes: 0

Related Questions