簡體   English   中英

在python中運行perl腳本並將輸出存儲在文件中

[英]Running a perl script in python and storing the output in a file

我想跑

script.pl < input > output

在python中。 我試過這個:

subprocess.check_call(["script.pl","<","input",">","output"])

我猜這不是正確的方法。 任何幫助將非常感激!

您想要執行shell命令

script.pl < input > output             # 0 args, redirects stdin and stdout

但是你正在執行shell命令

script.pl "<" input ">" output         # 4 args, and no redirections

如果要運行shell命令,則需要運行shell。

subprocess.check_call(["/bin/sh", "-c", "script.pl < input > output"])
  -or-
subprocess.check_call("script.pl < input > output", shell=True)

如果你完全避免運行shell,那將是最好的。

in_fh  = open('input', 'r')
out_fh = open('output', 'w')
subprocess.check_call("script.pl", stdin=in_fh, stdout=out_fh)

把事情簡單化,

import os
os.system("script.pl < input > output")

暫無
暫無

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

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