gpt4 book ai didi

python - 使用 subprocess.Popen() 时,stderr 和 stdout 没有输出

转载 作者:太空狗 更新时间:2023-10-29 20:54:42 27 4
gpt4 key购买 nike

我正在使用 Python 自动执行 SVN 提交,我想将 SVN 命令的输出写入日志文件。我拥有的代码可以使 SVN 运行,但问题是在成功提交时,subprocess 调用不会为我的日志返回任何输出。

相比之下,当我手动运行 SVN 时,我得到的输出显示了命令的进度并显示了正在提交的文件。这就是我想要在我的日志文件中的内容。 SVN 是否将该数据输出到缓冲区而不是 stdout 或 stderr?如何为我的日志捕获该数据?

这是我使用的代码:

cmd = "svn commit --non-interactive --no-auth-cache -m 'Automatic commit' ./"
process = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
result = process.wait()

# Output
out = process.stdout.read()
err = process.stderr.read()

当我运行这段代码并且提交成功时,outerr 变量都是空的。

最佳答案

在使用 PIPE 时不要使用 wait()。使用 communicate()

process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, universal_newlines=True)

out, err = process.communicate()

来自subprocess docs :

Warning
Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

关于python - 使用 subprocess.Popen() 时,stderr 和 stdout 没有输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10406257/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com