How do I apply my python code to all of the files in a folder at once, and how do I create a new name for each subsequent output file?
Question: The code I am working with takes in a .pdf file, and outputs a .txt file. My
question is, how do I create a loop (probably a for loop) which runs the code
over and over again on all files in a folder which end in ".pdf"? Furthermore,
how do I change the output each time the loop runs so that I can write a new
file each time, that has the same name as the input file (ie. 1_pet.pdf >
1_pet.txt, 2_pet.pdf > 2_pet.txt, etc.)

Here is the code so far:

    
    
    path="2_pet.pdf"
    content = getPDFContent(path)
    encoded = content.encode("utf-8")
    text_file = open("Output.txt", "w")
    text_file.write(encoded)
    text_file.close()
    


Answer: One way to operate on all PDF files in a directory is to invoke `glob.glob()`
and iterate over the results:

    
    
    import glob
    for path in glob.glob('*.pdf')
        content = getPDFContent(path)
        encoded = content.encode("utf-8")
        text_file = open("Output.txt", "w")
        text_file.write(encoded)
        text_file.close()
    

Another way is to allow the user to specify the files:

    
    
    import sys
    for path in sys.argv[1:]:
        ...
    

Then the user runs your script like `python foo.py *.pdf`.

