Python subprocess.Popen.wait() returns 0 even though an error occured

2024/9/8 12:09:41

I am running a command line utility via Python's subprocess module. I create a subprocess.Popen() object with the command line arguments and stdout=subprocess.PIPE and then I use subprocess.wait() to wait until the task is complete and to return the return code which should indicate whether the task completed successfully.

translate = subprocess.Popen(['gdal_translate', '-of', 'HFA', 'inDataset', 'outDataset'], stdout=subprocess.PIPE)
if translate.wait() == 0: print "Success"
else: print "Fail - %s" % translate.stdout.read()

If I run my script in the windows command prompt I can see the messages provided by gdal_translate and they say there were errors that cause a process to fail, but my script still prints "Success".

How can I check for errors reported by the command line utility if the return code is always 0?

Answer

It is possible that you get errors being outputted and still have a return code of zero. In your code you only capture standard out and not standard error. In the run function below it will run a command, wait for execution to end, then read the returncode standard error and standard output. If there is anything on standard error or if returncode is not zero then it will treat that as a failure. You can see the four example calls in the code. The first one is a normal successful call, the second has return code 0 but has output on error and standard output. The third has a non-zero return code and error output, while the last example has a none-zero return code with no output at all.

code

from subprocess import Popen, PIPEdef run(cmd):print '-'*40print 'running:', cmdp = Popen(cmd, stderr=PIPE, stdout=PIPE, shell=True)output, errors = p.communicate()print [p.returncode, errors, output]if p.returncode or errors:print 'something went wrong...'run("echo all is well")
run("echo out;echo error 1>&2")
run("this-will-fail")
run("exit 1")

output

----------------------------------------
running: echo all is well
[0, '', 'all is well\n']
----------------------------------------
running: echo out;echo error 1>&2
[0, 'error\n', 'out\n']
something went wrong...
----------------------------------------
running: this-will-fail
[127, '/bin/sh: this-will-fail: not found\n', '']
something went wrong...
----------------------------------------
running: exit 1
[1, '', '']
something went wrong...
https://en.xdnf.cn/q/72643.html

Related Q&A

Better way to call a chain of functions in python?

I have a chain of operations which needs to occur one after the other and each depends on the previous functions output.Like this:out1 = function1(initial_input) out2 = function2(out1) out3 = function3…

Pytest text annotation for test with tuple of parameters

Im looking for more elegant solution for this kind of problem:def ids(x):if isinstance(x, int):return str(x)elif isinstance(x, str):return x[0]@pytest.mark.parametrize("number, string",[(1, &…

OpenCV wont capture from MacBook Pro iSight

Since a couple of days I cant open my iSight camera from inside an opencv application any more. cap = cv2.VideoCapture(0) returns, and cap.isOpened() returns true. However, cap.grab() just returns fals…

SQLAlchemy filter on list attribute

I have the following model defined with Flask-SQLAlchemy:"""models.py"""from flask_sqlalchemy import SQLAlchemydb = SQLAlchemy()skill_candidate = db.Table(SkillCandidate,d…

How to convert an InMemoryUploadedFile in django to a fomat for flickr API?

I have a class that uploads a file to Flickr. The file is of type InMemoryUploadedFile.I would like to know how to convert or pass the data in the InMemoryUploadedFile file, to a format for flickrs API…

Speed up Metropolis-Hastings algorithm in Python

I have some code that samples a posterior distribution using MCMC, specifically Metropolis Hastings. I use scipy to generate random samples:import numpy as np from scipy import statsdef get_samples(n):…

How to properly use Rules, restrict_xpaths to crawl and parse URLs with scrapy?

I am trying to program a crawl spider to crawl RSS feeds of a website and then parsing the meta tags of the article.The first RSS page is a page that displays the RSS categories. I managed to extract t…

sudo required for easy_install pip in OS X Lion?

Im coming from Snow Leopard at work to a Lion installation at home. I do NOT remember having to:sudo easy_install pipIs that required for Lion? I got errors until I did that, and pip ended up here:[…

CUDNN_STATUS_NOT_INITIALIZED when trying to run TensorFlow

I have installed TensorFlow 1.7 on an Ubuntu 16.04 with Cuda 9.0 and CuDNN 7.0.5 and vanilla Python 2.7 and although they samples for both CUDA and CuDNN run fine, and TensorFlow sees the GPU (so some …

What is the correct way to switch freely between asynchronous tasks?

Suppose I have some tasks running asynchronously. They may be totally independent, but I still want to set points where the tasks will pause so they can run concurrently. What is the correct way to run…