Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiple lines
#1
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.
Reply
#2
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?
Reply
#3
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
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#4
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))
Reply
#5
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
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write the condition for deleting multiple lines? Lky 3 1,172 Jul-10-2022, 02:28 PM
Last Post: Lky
  Delete multiple lines from txt file Lky 6 2,340 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Display table field on multiple lines, 'wordwrap' 3python 0 1,788 Aug-06-2021, 08:17 PM
Last Post: 3python
  pulling multiple lines from a txt IceJJFish69 3 2,606 Apr-26-2021, 05:56 PM
Last Post: snippsat
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,929 Aug-10-2020, 11:01 PM
Last Post: medatib531
  print python json dump onto multiple lines lhailey 2 19,935 Mar-02-2020, 12:47 PM
Last Post: vishalhule
  Random nr. no repetition & printing multiple lines Joey 7 2,840 Feb-05-2020, 04:23 PM
Last Post: Larz60+
  how to insert # to multiple lines? hkfatasy 1 2,919 Dec-22-2019, 01:51 AM
Last Post: ichabod801
  KeyError -read multiple lines bongielondy 2 3,472 Nov-06-2019, 01:33 AM
Last Post: bongielondy
  spliting similar but multiple lines anna 7 3,576 Apr-20-2019, 08:53 AM
Last Post: anna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020