【发布时间】:2022-06-11 12:36:46
【问题描述】:
我正在尝试使用 os.popen、subprocess.run、subprocess.Popen 函数获取最后一个 python 输出,正如这个古老的问题 How can I get terminal output in python? 中所示 它似乎不起作用。
基本上我想做的是检测最后一个输出,例如:
print("Hello World")
last_output = get_last_output()
print() # For avoiding confutions
print(last_output)
# Would print "\n"
print("Hello World", end="")
last_output = get_last_output()
print() # For avoiding confutions
print(last_output)
# Would print "Hello World"
我也希望这个遮阳篷可以独立于控制台工作
【问题讨论】:
-
你想要
subprocess.check_output吗?它基本上是Popen,但它会同步并阻止你的代码,直到它完成。 -
它“有效”或“无效”似乎有效?如果它“不起作用”,究竟是什么不起作用?
-
@EricJin 在这种情况下你会如何使用它?我不明白它的文档docs.python.org/3/library/…
-
@YevhenKuzmovych 他们不打印文件中的最新输出,他们只是打印当前文件路径的字节数
-
我现在明白了,所以您想将最后一个运行的函数的输出捕获到标准输出?你需要阅读
sys.stdout。
标签: python printing console output