0

I am creating a large set of math tests with question that have random numbers generated and I want to be able to save multiple copies, push them to latex and get them saved all within one python code. I understand the python os.system command and the subprocess.run command can do this, but I am not able to figure out how to get the file into the command because it iterates. I currently am trying to use a string to accomplish this task.

for i in range(1, 11):
    f = open("test_" + str(i).zfill(2) + ".tex", 'w+')
    ##### run test building code here ######
    f.close()
    #subprocess.run("pdflatex f.name")  #<--- How do I get this to run for each of the 10 files 

I can run this with the actual file name, so I know it's not hard, but I can't seem to figure out how to pass in the string file name.

1 Answers1

0

Build the name beforehand, use an f-string to pass it in (assuming you're using Python 3.6+, otherwise see here). Note that the f in front of the string has nothing to do with the file f.

for i in range(1, 11):
    file_path = "test_" + str(i).zfill(2) + ".tex"
    f = open(file_path , 'w+')
    #####
    f.close()
    subprocess.run(f"pdflatex {file_path}")
Random Davis
  • 6,662
  • 4
  • 14
  • 24