Python Forum

Full Version: multiple lines
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Quick question. So in my batch file I have something like -par $var1=100 -par $var2=1000. I was wondering if I could do this in the loop using python. So let’s say I have five parameters to pass. Instead of typing it by hand -par $var1=100 -par $var2=200 -par $var3=300 -par $var4=400 -par $var5=500, could I do that within the batch/python file automatically? Let’s say user says it will have 5 parameter files for now.
Thank you so much in advance.
Yes, you can.

I'm not sure what more you're looking for. I'm not familiar with batch files (I know of them, but haven't coded one in over ten years). Are you asking how to pass parameters from the batch file to your Python script?
In Windows, assuming proper PATH Environment variables, to pass parameters, you can invoke Python as (similar to C):
py argcargv.py "embedded space" bb cc
or
python argcargv.py "embedded space" bb cc
or
You can 'Double Click' on the Python file (argcargv.py or equivalent) in 'File Explorer' (no extra parameters).

The following was tested on Windows 10 using Python 3.6.5. In Windows, parameters containing embedded spaces must be surrounded by double quote characters (e.g. "embedded space") .
import os
import sys

iparameter_count = len(sys.argv)
print("Number of parameters (including script name): ", iparameter_count)

print("Script name: ", sys.argv[0])
print("The parameters are: " , str(sys.argv))
print("")

for i, sParameter in enumerate(sys.argv, start=1):
    print ("Parameter {}: {}".format(i, sParameter))

spath = sys.argv[0]  
print("")
print("Folder name is '{}'".format(os.path.dirname(spath)))
print("Base file name is '{}'".format(os.path.basename(spath)))
print("")

#Pause only if the Program is Interactive - i.e. only 1 argument   
if iparameter_count <= 1 :
  programPause = input("Press the <ENTER> key to EXIT")
Output:
Number of parameters (including script name): 4 Script name: argcargv.py The parameters are: ['argcargv.py', 'embedded space', 'bb', 'cc'] Parameter 1: argcargv.py Parameter 2: embedded space Parameter 3: bb Parameter 4: cc Folder name is '' Base file name is 'argcargv.py'
Lewis
Hi Lewis,
this is great! Thank you. However, any suggestion on how can I pass line 12 (see below) in your code to batch file? Maybe as string? Thanks!
print ("Parameter {}: {}".format(i, sParameter))
You have to double up on the internal 'Double Quotes'.
python argcargv.py aaaa "print (""Parameter {}: {}"".format(i, sParameter))"

Output:
Number of parameters (including script name): 3 Script name: argcargv.py The parameters are: ['argcargv.py', 'aaaa', 'print ("Parameter {}: {}".format(i, sParameter))'] Parameter 1: argcargv.py Parameter 2: aaaa Parameter 3: print ("Parameter {}: {}".format(i, sParameter)) Folder name is '' Base file name is 'argcargv.py'
Lewis

P.S. The Python 'Syntax Police' indicate that 'print (' should be 'print(' (i.e. no space). Smile