简体   繁体   English

使用特定参数在python中运行shell脚本

[英]Run shell script in python with specific parameters

I wish to run a script, lets call it api.sh .我想运行一个脚本,我们称之为api.sh The script takes various arguments,该脚本采用各种参数,

  • -t token -t 令牌
  • -r rules.json -r 规则.json
  • -s data.json -s 数据.json

and it is going to create a new json file, eg data_2.json .它将创建一个新的 json 文件,例如data_2.json

When I run this in terminal I use the following command:当我在终端中运行它时,我使用以下命令:

./api.sh -t token -r rules.json -s data.json > data_2.json 

However, I wish to run this command line in Python.但是,我希望在 Python 中运行此命令行。 Any suggestions are appreciated.任何建议表示赞赏。

Thanks,谢谢,

I don't know if it supports python but you can use getopts .我不知道它是否支持 python 但你可以使用getopts

Look here这里

Does this test.py work:这个test.py工作吗:

import subprocess
from subprocess import Popen

path_to_output_file = 'data_2.json'

myoutput = open(path_to_output_file,'w+')

p = Popen(["./api.sh", "-t" , "token", "-r", "rules.json", "-s", "data.json"], stdout=myoutput, stderr=subprocess.PIPE, universal_newlines=True)

output, errors = p.communicate()

You can refer to this for details.你可以参考这个了解详情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM