Jack Olien
Jack Olien

Reputation: 1

Repeat one python script with different inputs 1 to 100

I have python script a.py and it takes integer input I want to repeat this script with different input integers from 1 to 10. Usually I do create a1.py to a10.py with integer hard-corded but i want to run them without creating a1.py to a10.py

Edit:

I have script test.py

import sys
count = sys.argv[1]
print(count)

#I use this count value as an input for running further script

I want to run python test.py 2 after python test.py 1 finishes and so on. So I naively use this command: python test.py 1 && python test.py 2 && python test.py 3 && and so on... up to python test.py 10

Is there any short way to write this long command.

Upvotes: 0

Views: 149

Answers (2)

Dmitry Rusanov
Dmitry Rusanov

Reputation: 551

You can use sys.argv for getting parameters. And run the file passing the parameter to the function python my_file.py 10.

import sys


def run_range():
    count = sys.argv[1]
    for i in range(int(count)):
        print(i)


if __name__ == '__main__':
    run_range()

Upvotes: 1

Arib Muhtasim
Arib Muhtasim

Reputation: 159

You can do something similar to this:

def myFunc(x):
    print(f"HALLO {x}")

for x in range(0, 11):
    myFunc(x)

Upvotes: 0

Related Questions