0

My research group requires me to run a bunch of different test cases for several different problems. All in all, there are 486 different files with 81 for each problem. I know very little about Python and all I've managed to do so far is get it to run one test file. The command to run the problem solver from the command line is this:

python Solver.py Tests/testName.txt

Of course, I don't plan on typing out the name of each file over and over because that takes far too long. Is there any way for me to run this command for every file in that Tests folder? The files are all named using the same format, where it's something like [original_filename]_[Precision][Criteria][SpeculationLevel][PreconditionLevel].txt, where precision, criteria, speculationLevel, and preconditionLevel each have three possible values. I originally wrote the files using nested for loops but that was in Java.

4
  • 1
    You could incorporate the file handling into the code itself, or write a script that runs the python file for each test file. Commented Feb 28, 2014 at 17:29
  • python Solver.py Test/*.txt Commented Feb 28, 2014 at 17:30
  • I got an error from running that line. The last line of it reads, "IOError: [Errno 22] invalid mode ('w+') or filename: 'Tests/*.txt.tmp' Commented Feb 28, 2014 at 17:35
  • Are you on Windows? That may be why python Solver.py Test/*.txt doesn't work. Commented Feb 28, 2014 at 20:38

2 Answers 2

1

I would write a script that runs it for you.

import glob
import subprocess as sub

for file in glob.glob("Tests/*.txt"): # or however you want to build the list
    sub.call(["python","Solver.py",file])
Sign up to request clarification or add additional context in comments.

3 Comments

So *.type specifies all files of some type in a directory?
@user3308219 * is usually used as a glob character. glob.glob("Tests/*.txt") will give you a list with all the files in it that match that file path.
@user3308219 for example, "cat*" would match cat,catatonic,category, catherine, etc.
0

Seems like adsmith's answer with glob.glob should've worked for you (on Windows, too), but here is another thing to try. It'd be run by passing a folder name as the argument: python runner.py Tests

import fnmatch, os, subprocess, sys
folder = sys.argv[1]
for filename in fnmatch.filter(os.listdir(folder), '*.txt'):
    subprocess.call(['python', 'Solver.py', filename])

Comments

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.