Python subprocess with stdout redirect returning an int

我只是一个虾纸丫 提交于 2019-12-05 10:20:20

subprocess.call just runs the command and returns its exit status (In python the exit status can be set by sys.exit(N) -- In other languages the exit status is determined by different means). If you want to actually get a handle on the process, you'll need to use subprocess.Popen. So, for your example:

run = subprocess.Popen('Name', stdout = subprocess.PIPE, env={'LANG':'C++'})
data, error = run.communicate()

The programs exit status is now available through the returncode attribute.

Also, as a matter of style, I would either do:

run = subprocess.Popen('Name', stdout = subprocess.PIPE, stderr = subprocess.PIPE, env={'LANG':'C++'})
data, error = run.communicate()

or:

run = subprocess.Popen('Name', stdout = subprocess.PIPE, env={'LANG':'C++'})
data, _ = run.communicate()

Since you're not giving yourself the ability to capture stderr, you probably shouldn't pretend like you got something meaningful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!