Search code examples
pythonargvsys

Python : How to pass sys.argv to the list of .py files?


I have a below code where I am trying to pass sys.argv[1] to all my python scripts using date as parameter. The objective is to run from the command line. But its not picking up the date inside file1.py, file2.py and file3.py. Please suggest.

command line: python C:\user\auto.py 03-Jan-2020

#code: auto.py

date = str(sys.argv[1])
print(sys.argv[1])
#reading python scripts
scripts_list = [r'C:\user\file1.py', r'C:\user\file2.py', r'C:\user\file3.py']

output_list = [r'C:\user\out1.py', r'C:\user\out2.py', 
r'C:\user\out3.py']

input_processes = []
output_processes = []

for script in scripts_list:
    p = subprocess.Popen(['python', script])
    input_processes.append(p)    

for p in input_processes:
    p.wait()

for script in output_list:
    p = subprocess.Popen(['python', script])
    output_processes.append(p)    

for p in output_processes:
    p.wait()

Solution

  • You can iterate through your list calling each script and passing the arg.

    import subprocess
    for s in scripts_list:
        subprocess.run(['python', s, date])