我有一个命令行工具(实际上是几个),我正在用Python编写包装器.
该工具通常使用如下:
$ path_to_tool -option1 -option2 > file_out
Run Code Online (Sandbox Code Playgroud)
用户将输出写入file_out,并且还能够在工具运行时查看该工具的各种状态消息.
我想复制此行为,同时还将stderr(状态消息)记录到文件中.
我有的是这个:
from subprocess import call
call(['path_to_tool','-option1','option2'], stdout = file_out, stderr = log_file)
Run Code Online (Sandbox Code Playgroud)
除了stderr没有写入屏幕之外,这工作正常.我当然可以添加代码来将log_file的内容打印到屏幕上,但是用户将在完成所有操作后看到它,而不是在它发生时.
总结一下,期望的行为是:
我有一种感觉,我要么错过了一些非常简单的东西,要么这比我想象的要复杂得多......感谢您的帮助!
编辑:这只需要在Linux上工作.
我了解到在Python中执行命令时,我应该使用子进程.我想要实现的是通过ffmpeg编码文件并观察程序输出直到文件完成.Ffmpeg将进度记录到stderr.
如果我尝试这样的事情:
child = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE)
complete = False
while not complete:
stderr = child.communicate()
# Get progress
print "Progress here later"
if child.poll() is not None:
complete = True
time.sleep(2)
Run Code Online (Sandbox Code Playgroud)
调用child.communicate()并等待命令完成后,程序不会继续.还有其他方法可以跟随输出吗?
我尝试完成的任务是流式传输ruby文件并打印出输出.(注意:我不想一次打印出所有内容)
main.py
from subprocess import Popen, PIPE, STDOUT
import pty
import os
file_path = '/Users/luciano/Desktop/ruby_sleep.rb'
command = ' '.join(["ruby", file_path])
master, slave = pty.openpty()
proc = Popen(command, bufsize=0, shell=True, stdout=slave, stderr=slave, close_fds=True)
stdout = os.fdopen(master, 'r', 0)
while proc.poll() is None:
data = stdout.readline()
if data != "":
print(data)
else:
break
print("This is never reached!")
Run Code Online (Sandbox Code Playgroud)
ruby_sleep.rb
puts "hello"
sleep 2
puts "goodbye!"
Run Code Online (Sandbox Code Playgroud)
问题
流文件工作正常.hello/goodbye输出以2秒延迟打印.正如脚本应该工作.问题是readline()最后会挂起而永不退出.我从未到过最后一个印刷品.
我知道有很多这样的问题,这里有一个stackoverflow但是没有它们让我解决问题.我不是那个整个子流程的东西,所以请给我一个更实际/具体的答案.
问候
编辑
修复意外的代码.(与实际错误无关)
是否可以修改以下代码以从'stdout'和'stderr'打印输出:
代码:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import subprocess
def run_cmd(command, cwd=None):
p = subprocess.Popen(command, cwd=cwd, shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
outs, errs = p.communicate()
rc = p.returncode
outs = outs.decode('utf-8')
errs = errs.decode('utf-8')
return (rc, (outs, errs))
Run Code Online (Sandbox Code Playgroud)
感谢@unutbu,特别感谢@ jf-sebastian,最终功能:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from queue import Queue
from subprocess import PIPE, Popen
from threading import Thread
def read_output(pipe, funcs):
for line in iter(pipe.readline, b''):
for func in funcs: …Run Code Online (Sandbox Code Playgroud) 我有一个程序写入stdout和stderr.我想从python运行它,捕获stdout和stderr.我的代码看起来像:
from subprocess import *
p = Popen( exe, shell=TRUE, stdout=PIPE, stderr=PIPE )
rtrncode = p.wait()
Run Code Online (Sandbox Code Playgroud)
对于几个程序,这很好,但是当我添加一个新程序时,新的程序会永远挂起.如果我删除stdout=PIPE,程序将其输出写入控制台并完成,一切都很好.如何确定导致挂起的原因?
在Windows XP上使用python 2.5.该程序不从stdin读取,也没有任何类型的用户输入(即"按键").
我试图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.
为什么是这样?这正是我见过的大多数示例都是他们的代码,但它没有读取文件.
编辑:
有没有办法让它只在有东西被阅读时才能运行?
我正在运行一个脚本,通过使用执行许多可执行文件
subprocess.call(cmdArgs,stdout=outf, stderr=errf)
Run Code Online (Sandbox Code Playgroud)
when outf/ errf是None或文件描述符(stdout/的不同文件stderr).
有什么方法可以执行每个exe,以便将stdout和stderr一起写入文件和终端?
我正在使用子进程模块调用外部程序(plink.exe)来登录服务器; 但是当我呼叫通信来读取输出时,它就是阻塞.代码如下:
import subprocess
process = subprocess.Popen('plink.exe hello@10.120.139.170 -pw 123456'.split(), shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print process.communicate() #block here
Run Code Online (Sandbox Code Playgroud)
我知道该块是因为plink.exe它仍在运行; 但我需要在子进程终止之前读取输出.反正有吗?
我试图在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)