簡體   English   中英

Python:(Perl)子過程無輸出

[英]Python: no output from (Perl) subprocess

因此,我想從Python中激活一個Perl腳本,經過一段時間后,我進入了執行該腳本的階段,但是沒有任何輸出,我也不知道這是怎么回事。

我什至不知道它是否可以識別腳本或輸入文件,因為它可以接受所有內容,並且不顯示任何錯誤消息。

script = subprocess.Popen(["perl" , "C:\\Users\\...\\pal2nal.pl" , "C:\\Users\\...\\input_file" , "C:\\Users\\...\\output_file" ], stdout=subprocess.PIPE)

while True:
    line = script.stdout.readline()
    if line == b'' and script.poll() != None:
        break
    sys.stdout.write(line.decode('utf-8'))
    sys.stdout.flush()

output = script.communicate()[0]
exitCode = script.returncode 

如果有人感興趣,這是腳本。http://www.bork.embl.de/pal2nal/distribution/pal2nal.v14.tar.gz

這是我第一次使用子流程,並且嘗試過錯誤檢查,但最終沒有成功。

要在子流程輸出到達時逐行顯示並將其捕獲到變量中,請執行以下操作:

#!/usr/bin/env python3
import io
from subprocess import Popen, PIPE

lines = []
with Popen(["perl" , r"C:\Users\...\pal2nal.pl", 
            r"C:\Users\...\input_file", 
            r"C:\Users\...\output_file"],
           stdout=PIPE, bufsize=1) as p:
    for line in io.TextIOWrapper(p.stdout, encoding='utf-8'):
        print(line, end='')
        lines.append(line)
output = ''.join(lines)
print(len(lines), p.returncode)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM