我正在尝试用Python进行系统调用,并将输出存储到我可以在Python程序中操作的字符串中.
#!/usr/bin/python
import subprocess
p2 = subprocess.Popen("ntpq -p")
Run Code Online (Sandbox Code Playgroud)
我尝试了一些事情,包括一些建议:
但没有任何运气.
我正在使用子进程模块从python运行二进制文件.
要捕获二进制文件生成的输出,我使用:
proc = subprocess.Popen (command_args, shell=False, stdout=subprocess.PIPE)
out = proc.communicate()[0]
#print the output of the child process to stdout
print (out)
Run Code Online (Sandbox Code Playgroud)
这样做是在完成执行后打印进程的输出.无论如何我可以在程序执行时将此输出打印到stdout吗?我真的需要看看输出是什么,因为这些程序可以运行很长时间.
谢谢您的帮助.
可能重复:
管道子进程标准输出到变量
我正在运行python程序:
import os
os.system("ls") # ls command runs on the terminal
Run Code Online (Sandbox Code Playgroud)
要将输出存储在文件中:
os.system("ls > a.txt")
Run Code Online (Sandbox Code Playgroud)
我需要的是,它将输出存储在一些临时字符串中.那可能吗 ??