Jaff
Jaff

Reputation: 185

Running python script multiple times with different arguments read from a text file

So i have a python script that takes an argument of a location and runs the script accordingly.

I want to speed up the process so rather than waiting for each run to finish and then entering the second argument (location).

Is there a way to enter all arguments in a text file like so:

    C:\a\b
    C:\a\c
    C:\a\d

and then get python to run each one individually?

Thank you.

Upvotes: 0

Views: 1057

Answers (2)

Taku
Taku

Reputation: 33714

you can make a loop and iterate through the file names. So it will be something like this:

with open("arguments.txt") as fp:
    for filename in fp:
        with open(filename.strip()) as file:
            # do stuff with the file

EDIT:

with open("arguments.txt") as fp:
    for filename in fp:
        filename = filename.strip()
        # now you can pass the filename to your function

Upvotes: 1

dl.meteo
dl.meteo

Reputation: 1766

If i understand your question right, you want to enter arguments that are listed in directory?

For that purpose you can use os with the following commands

from os import listdir
from os.path import isfile, join
mypath = 'C:\a\'
files = [f for f in listdir(mypath) if isfile(join(mypath, f))]
files
>>> ['C:\a\a', 'C:\a\b', 'C:\a\c']

This will give you a list of directory locations.

Upvotes: 1

Related Questions