0

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.

0

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

14 Comments

I have tried this but i am getting the following error when i run it: IOError: [Errno 22] invalid mode ('r') or filename: '"\\\\lGup-002\\SFQ\\brands\\Releases\\ss\\16.43\\Intenal 7\\15-09-2016"\n'
Yea try it now, I fixed it when your typing the traceback, sorry forgot to strip() And you don't need " when you're storing the filenames
I added .strip() method but I receive the same error.
and did you get rid of the quotation marks inside the file of filenames? To make it simple, how did you store them?
use a / instead of \\ to make it simple
|
1

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.

2 Comments

Sorry for not being clear, the directory is the argument
Now you only have to loop through the files in your python script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.