Python subprocess with dynamic variables and arguments

Question:

I want to ask how can I run subprocess.run() or subprocess.call() in python when the arguments are dynamic. I have already stored all commands in an external batch file, and I want to use Python to run the batch file after I update the arguments. I will give more details in the following:

the batch file is like this:

echo on

set yyyy=%1
set mm=%2
set dd=%3
set rangePath=%4
set pulPath=%5
set stasDate=%6

'''Command to run the batch file'''

My code right now is:

param_temp = 'pathway for parameter input yaml file'+'param_input.yml'
param = yaml.safe_load(param_temp)

yyyy = str(param['Year'])
mm = str(param['Month'])
dd = str(param['Date'])
rangePath = param['Path1']
pulPath = param['Path2']
stasDate = str(param['yyyymmdd'])

param_str = (yyyy+'_'+mm+'_'+dd+'_'+rangePath+'_'+pulPath+'_'+stasDate).split('_')
subprocess.call(param_str + [str('batch file path')], shell=False)

I create a YAML file to include all my parameters and intend to only adjust my YAML file if I want to run the batch file for different date. I choose the param_str because I googled it, and the web told me that %1 represents the variable or matched string after entering the batch file name. However, the approach is either invalid or outputs nothing. Can someone give me some ideas on how to do this, please?

Asked By: Yuuuu

||

Answers:

A slightly simpler implementation might look like:

#!/usr/bin/env python
import yaml

param_temp = 'pathway for parameter input yaml file'+'param_input.yml'
batch_path = 'batch file path'

param = yaml.safe_load(param_temp)
params = [ 'Year', 'Month', 'Date', 'Path1', 'Path2', 'yyyymmdd' ]
param_values = [ str(param[p]) for p in params ]

subprocess.call([batch_path] + param_values)

Note:

  • The path to the script to invoke comes before the arguments to that script.
  • We aren’t generating an underscore-separated string only to split it into a list — it’s more sensible (and less error-prone) to just generate a list directly.
  • shell=False is default, so it doesn’t need to be explicitly specified.
Answered By: Charles Duffy
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.