我正在编写使用一些Perl脚本的Python脚本,但其中一个使用stdout,所以我必须在bash中使用重定向>
将此输出写入文件。构建一个运行Perl脚本的Python脚本。如何重定向标准输出?
所有输入和输出文件都是文本文件。
# -*- coding: utf-8 -*-
import subprocess
filename = input("What the name of the input? ")
#STAGE 1----------------------------------------------------------------------
subprocess.Popen(["perl", "run_esearch.pl", filename , 'result'])
#STAGE 2----------------------------------------------------------------------
subprocess.Popen(["perl", "shrink.pl", 'result'])
'''Here the input from stage one is "shrunk" to smaller file, but
the output is printed to the console. Is it possible to write this out
to a file in Python, so I can use it in stage 3? '''
#STAGE 3----------------------------------------------------------------------
subprocess.Popen(["perl", "shrink2.pl", 'stdoutfromstage2'])
是'中间文件shrink2.pl'能够直接从标准输入中读取,还是必须以文件名作为参数? (也就是说,你能从命令行运行'perl shrink.pl result | perl shrink2.pl'吗?) – chepner
必须从cmd行中获取参数 –