简体   繁体   中英

Get error thrown by command-line execution using subprocess python

I am trying to retrive error(exit code importantly) thrown by any command line execution in python script itself. I am using subprocess for this. When I execute any wrong commands then it throws an error in terminal as usual, but then it stops executing python file and I can't get store error.

Look at the code. p_status is supposed to store exit code. But before printing it stops the script after throwing error in terminal.

process = subprocess.Popen([<command>], stdout = subprocess.PIPE)
output = process.communicate()
p_status = process.wait()
print(p_status)

I went through different solutions and tried all of them but couldn't get the required result.

Got this problem solved using the following code:

try:
    subprocess.Popen([<command>], stdout = subprocess.PIPE)
except OSError as error:
    print(error.errno) #for exit code

PS - credits @karolch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM