[英]How do I push a subprocess.call() output to terminal and file?
I have subprocess.call(["ddrescue", in_file_path, out_file_path], stdout=drclog)
. 我有
subprocess.call(["ddrescue", in_file_path, out_file_path], stdout=drclog)
。 I'd like this to display the ddrescue in the terminal as it's running and write the output to the file drclog. 我希望在运行时在终端中显示ddrescue并将输出写入文件drclog。 I've tried using
subprocess.call(["ddrescue", in_file_path, out_file_path], stdout=drclog, shell=True)
, but that gives me an input error into ddrescue. 我试过使用
subprocess.call(["ddrescue", in_file_path, out_file_path], stdout=drclog, shell=True)
,但这给了我ddrescue输入错误。
If ddrescue
doesn't change its output if its stdout/stderr are redirected to a pipe then you could use tee
utility, to display output on the terminal and to save it to a file: 如果
ddrescue
的stdout / stderr重定向到管道时ddrescue
不会更改其输出,则可以使用tee
实用程序在终端上显示输出并将其保存到文件中:
$ ddrescue input_path output_path ddrescue_logfile |& tee logfile
If it does then you could try to provide a pseudo-tty using script
utility: 如果确实如此,那么您可以尝试使用
script
实用程序提供一个伪tty:
$ script -c 'ddrescue input_path output_path ddrescue_logfile' -q logfile
If it writes directly to a terminal then you could use screen
to capture the output: 如果它直接写到终端,则可以使用
screen
捕获输出:
$ screen -L -- ddrescue input_path output_path ddrescue_logfile
The output is saved in screenlog.0
file by default. 默认情况下,输出保存在
screenlog.0
文件中。
To emulate the tee
-based command in Python without calling tee
utility: 要在Python中模拟基于
tee
的命令而不调用tee
实用程序,请执行以下操作:
#!/usr/bin/env python3
import shlex
import sys
from subprocess import Popen, PIPE, STDOUT
command = 'ddrescue input_path output_path ddrescue_logfile'
with Popen(shlex.split(command), stdout=PIPE, stderr=STDOUT, bufsize=1) as p:
with open('logfile', 'wb') as logfile:
for line in p.stdout:
logfile.write(line)
sys.stdout.buffer.write(line)
sys.stdout.buffer.flush()
To call the tee
-based command in Python using shell=True
: 要使用
shell=True
在Python中调用基于tee
的命令:
#!/usr/bin/env python
from pipes import quote
from subprocess import call
files = input_path, output_path, ddrescue_logfile
rc = call('ddrescue {} | tee -a drclog'.format(' '.join(map(quote, files))),
shell=True)
To emulate the script
-based command: 要模拟基于
script
的命令:
#!/usr/bin/env python3
import os
import shlex
import pty
logfile = open('logfile', 'wb')
def read(fd):
data = os.read(fd, 1024) # doesn't block, it may return less
logfile.write(data) # it can block but usually not for long
return data
command = 'ddrescue input_path output_path ddrescue_logfile'
status = pty.spawn(shlex.split(command), read)
logfile.close()
To call screen
command in Python: 要在Python中调用
screen
命令:
#!/usr/bin/env python3
import os
import shlex
from subprocess import check_call
screen_cmd = 'screen -L -- ddrescue input_path output_path ddrescue_logfile'
check_call(shlex.split(screen_cmd))
os.replace('screenlog.0', 'logfile')
对我subprocess.call(["ddrescue $0 $1 | tee -a drclog", in_file_path, out_file_path], shell=True)
的解决方案是subprocess.call(["ddrescue $0 $1 | tee -a drclog", in_file_path, out_file_path], shell=True)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.