Home > OS >  Run shell script in python with specific parameters
Run shell script in python with specific parameters

Time:11-01

I wish to run a script, lets call it api.sh. The script takes various arguments,

  • -t token
  • -r rules.json
  • -s data.json

and it is going to create a new json file, e.g. 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. Any suggestions are appreciated.

Thanks,

CodePudding user response:

I don't know if it supports python but you can use getopts.

Look here

CodePudding user response:

Does this test.py work:

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.

  • Related