Python getstatusoutput替换未返回完整输出 [英] Python getstatusoutput replacement not returning full output

查看:308
本文介绍了Python getstatusoutput替换未返回完整输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Python 2 *中getstatusoutput()函数的一个很好的替换,该函数在Unix和Windows上同样有效.但是,我认为output的构造方式存在问题.它仅返回输出的最后一行,但我不知道为什么.任何帮助都会很棒.

I came across this great replacment for the getstatusoutput() function in Python 2.* which works equally well on Unix and Windows. However, I think there is something wrong with the way the output is constructed. It only returns the last line of the output, but I can't figure out why. Any help would be awesome.

def getstatusoutput(cmd):
    """Return (status, output) of executing cmd in a shell."""
    """This new implementation should work on all platforms."""
    import subprocess
    pipe = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, universal_newlines=True)
    output = "".join(pipe.stdout.readlines())
    sts = pipe.returncode
    if sts is None: sts = 0
    return sts, output

推荐答案

有一个错误.

通话:

pipe = subprocess.Popen(...)

启动该过程.该行:

output = "".join(pipe.stdout.readlines())

读取进程的标准输出(如果输出错误,则可能被阻塞,但是由于您未重定向stderr,因此这是安全的;在更一般的情况下,您需要使用.communicate()函数以避免可能的楔入) .您可能会想:好吧,既然我已经阅读了所有输出,则子进程显然已经完成,因此应该设置pipe.returncode.但实际上,如果您替换:

reads the process's stdout (if there were error output it could get jammed up, but since you did not redirect stderr this is safe; in the more general case, you need to use the .communicate() function to avoid possible wedging). You might think: well, now that I've read all the output, the subprocess has clearly finished, so pipe.returncode should be set. But in fact, if you replace:

sts = pipe.returncode
if sts is None: sts = 0

使用包含某种诊断(或完全删除第二行)的代码,您会发现,至少在某些系统上,有时sts None.原因是subprocess模块还没有机会检索它.您应该将这些行替换为:

with code that includes a diagnostic of some sort (or just delete the second line entirely), you'll find that, at least on some systems sometimes, sts is None. The reason is that the subprocess module has not had a chance to retrieve it yet. You should replace those lines with:

sts = pipe.wait()

收集结果,而无需进行is None测试. (即使您已经使用过.communicate()和/或已经调用过.wait(),也可以安全地调用.wait().)

to collect the result and obviate the need for the is None test. (It's safe to call .wait() even if you've used .communicate() and/or already called .wait().)

只需使用以下命令,"".join(...)的处理效率就会更高:

The "".join(...) can be done slightly more efficiently by using just:

output = pipe.stdout.read()

所有这些,它确实为我提供了完整的输出.也许您正在从仅使用'\r'作为换行符的内容中读取内容,并且您的Python是在没有通用换行符支持的情况下构建的? (如果我运行一些生成\r -for-newline的东西,我仍然会得到所有的行,并用实际的换行符分隔.)

All that said, it does get the full output for me. Perhaps you're reading from something that uses just '\r' for newlines and your Python was built without universal-newline support? (If I run something that produces \r-for-newline I still get all the lines, separated by actual newlines.)

这篇关于Python getstatusoutput替换未返回完整输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆