我正在編寫使用一些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行中獲取參數 –