如何在Python脚本中调用外部命令(就像我在Unix shell或Windows命令提示符下键入它一样)?
我正在使用Python subprocess.communicate()来从一个运行大约一分钟的进程中读取stdout.
如何stdout以流式方式打印出该流程的每一行,以便我可以看到生成的输出,但在继续之前仍然阻止流程终止?
subprocess.communicate() 似乎立刻提供所有输出.
我正在使用Popen调用一个shell脚本,该脚本不断将其stdout和stderr写入日志文件.有没有办法连续同时输出日志文件(到屏幕),或者让shell脚本同时写入日志文件和标准输出?
我基本上想在Python中做这样的事情:
cat file 2>&1 | tee -a logfile #"cat file" will be replaced with some script
Run Code Online (Sandbox Code Playgroud)
再次,这将stderr/stdout连接到tee,将它写入stdout和我的日志文件.
我知道如何在Python中将stdout和stderr写入日志文件.我被困在哪里是如何将这些复制回屏幕:
subprocess.Popen("cat file", shell=True, stdout=logfile, stderr=logfile)
Run Code Online (Sandbox Code Playgroud)
当然我可以做这样的事情,但有没有办法在没有tee和shell文件描述符重定向的情况下做到这一点?:
subprocess.Popen("cat file 2>&1 | tee -a logfile", shell=True)
Run Code Online (Sandbox Code Playgroud) 我正在编写一个python脚本,它使用subprocess.Popen来执行两个程序(来自编译的C代码),每个程序生成stdout.该脚本获取该输出并将其保存到文件中.因为输出有时足以压倒subprocess.PIPE,导致脚本挂起,所以我将stdout直接发送到日志文件.我想让我的脚本写一些文件的开头和结尾,以及两个subprocess.Popen调用之间.但是,当我查看我的日志文件时,我从脚本写入日志文件的所有内容都在文件顶部,然后是所有可执行文件stdout.如何将添加的文本交错到文件?
def run(cmd, logfile):
p = subprocess.Popen(cmd, shell=True, universal_newlines=True, stdout=logfile)
return p
def runTest(path, flags, name):
log = open(name, "w")
print >> log, "Calling executable A"
a_ret = run(path + "executable_a_name" + flags, log)
print >> log, "Calling executable B"
b_ret = run(path + "executable_b_name" + flags, log)
print >> log, "More stuff"
log.close()
Run Code Online (Sandbox Code Playgroud)
日志文件具有:调用可执行文件A调用可执行文件B更多内容[...来自两个可执行文件的标准输出...]
例如,有没有办法在调用Popen后将A的stdout刷新到日志中?还有一件事可能是相关的:可执行A开始然后在B上,并且在B打印完成后,A然后打印更多东西并完成.
我在RHE Linux上使用Python 2.4.
我有以下大量的Python代码(运行v2.7)导致在MemoryError处理大(几GB)文件时抛出异常:
myProcess = Popen(myCmd, shell=True, stdout=PIPE, stderr=PIPE)
myStdout, myStderr = myProcess.communicate()
sys.stdout.write(myStdout)
if myStderr:
sys.stderr.write(myStderr)
Run Code Online (Sandbox Code Playgroud)
在阅读文档时Popen.communicate(),似乎有一些缓冲:
注意读取的数据缓冲在内存中,因此如果数据大小很大或不受限制,请不要使用此方法.
有没有办法禁用此缓冲,或强制缓存在进程运行时定期清除?
我应该在Python中使用什么替代方法来运行将千兆字节数据流式传输到的命令stdout?
我应该注意,我需要处理输出和错误流.
我试图stdout通过subprocess.Popen电话抓住,虽然我通过这样做很容易实现:
cmd = subprocess.Popen('ls -l', shell=True, stdout=PIPE)
for line in cmd.stdout.readlines():
print line
Run Code Online (Sandbox Code Playgroud)
我想stdout"实时" 抓住.使用上面的方法,PIPE正在等待抓取所有stdout,然后它返回.
因此,对于记录目的,这不符合我的要求(例如,"看到"发生时发生的事情).
有没有办法一行一行地stdout运行?或者这是一个限制subprocess(必须等到PIPE关闭).
编辑
如果我转readlines()了readline(),我只得到了最后一行stdout(不理想):
In [75]: cmd = Popen('ls -l', shell=True, stdout=PIPE)
In [76]: for i in cmd.stdout.readline(): print i
....:
t
o
t
a
l
1
0
4
Run Code Online (Sandbox Code Playgroud) 好的,我试图从python脚本运行一个C程序.目前我正在使用测试C程序:
#include <stdio.h>
int main() {
while (1) {
printf("2000\n");
sleep(1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
模拟我将要使用的程序,它不断地从传感器获取读数.然后我试图"2000"从C程序读取输出(在这种情况下)与python中的子进程:
#!usr/bin/python
import subprocess
process = subprocess.Popen("./main", stdout=subprocess.PIPE)
while True:
for line in iter(process.stdout.readline, ''):
print line,
Run Code Online (Sandbox Code Playgroud)
但这不起作用.从使用print语句开始运行该.Popen行然后等待for line in iter(process.stdout.readline, ''):,直到我按下Ctrl-C.
为什么是这样?这正是我见过的大多数示例都是他们的代码,但它没有读取文件.
编辑:
有没有办法让它只在有东西被阅读时才能运行?
我的python脚本(python 3.4.3)通过子进程调用bash脚本:
import subprocess as sp
res = sp.check_output("bashscript", shell=True)
Run Code Online (Sandbox Code Playgroud)
该bashscript包含以下行:
ssh -MNf somehost
Run Code Online (Sandbox Code Playgroud)
这将打开与某个远程主机的共享主连接,以允许一些后续操作.
执行python脚本时,它将提示该ssh行的密码,但在输入密码后它会阻塞,并且永远不会返回.当我按ctrl-C终止脚本时,我看到连接已正确建立(因此ssh行已成功执行).
使用时,我没有这个阻塞问题check_call,而不是check_output,但check_call不检索标准输出.我想了解究竟是什么导致阻塞行为check_output,可能与某些微妙的相关ssh -MNf.
我试图在Python中找到一种方法来运行其他程序:
这是我到目前为止所得到的...方法1:
def method1(command):
## subprocess.communicate() will give us the stdout and stderr sepurately,
## but we will have to wait until the end of command execution to print anything.
## This means if the child process hangs, we will never know....
proc=subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, executable='/bin/bash')
stdout, stderr = proc.communicate() # record both, but no way to print stdout/stderr in real-time
print ' ######### REAL-TIME ######### '
######## Not Possible
print ' ########## RESULTS ########## …Run Code Online (Sandbox Code Playgroud) 我试图在python中处理tcpdump输出.
我需要的是运行tcpdump(捕获数据包并给我信息)并读取输出并处理它.
问题是tcpdump一直在运行,我需要在输出后立即读取数据包信息并继续执行.
我试着查看python的子进程并尝试使用popen调用tcpdump并管道stdout但它似乎没有用.
关于如何进行此操作的任何指示.
import subprocess
def redirect():
tcpdump = subprocess.Popen("sudo tcpdump...", stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
while True:
s = tcpdump.stdout.readline()
# do domething with s
redirect()
Run Code Online (Sandbox Code Playgroud)