我可以在Python中记录子过程吗?

发布于 2025-02-08 19:30:19 字数 134 浏览 2 评论 0原文

我必须在python脚本内进行一些apt install命令,并且可以使用子过程。但是问题是我看不到安装进度(错误,完成,没有启动,正在进行等)。有没有一种方法可以像Bash一样打印apt日志?

I have to lanch some apt install commands inside a Python script, and this is possible using the subprocess. But the problem is that I can't see the installation progress (error, completed, didn't start, ongoing etc.). Is there a way to print the apt logs just like in bash?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨死的猪 2025-02-15 19:30:19

查看此示例。它使用popen的stdout使用iter()。由于您没有povide a代码,您如何调用APT GET我使用ping进行演示,但原理是相同的。

import subprocess

process = subprocess.Popen(["ping", "-c", "3", "google.com"], stdout=subprocess.PIPE, universal_newlines=True)
for line in iter(process.stdout.readline, ""):
    print(line)
process.stdout.close()
return_code = process.wait()

Check out this example. It uses iter() over stdout from Popen. Since you didn't povide a code how do you call apt get I used ping for demo but the principle is the same.

import subprocess

process = subprocess.Popen(["ping", "-c", "3", "google.com"], stdout=subprocess.PIPE, universal_newlines=True)
for line in iter(process.stdout.readline, ""):
    print(line)
process.stdout.close()
return_code = process.wait()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文