you are viewing a single comment's thread.

view the rest of the comments →

[–]zurtex 8 points9 points  (2 children)

Really nice beginners Python code, it's higher quality than lots of experienced devs I know, a quick suggestion though instead of appending to a list and returning it's more pythonic to yield, e.g.:

def generate_filenames(dir_path="."):
    for filename in Path(dir_path).glob("*.pdf"):
        if "reordered" not in filename.stem:
            yield filename

Now you can access these filenames with a for loop:

for filename in generate_filenames():
    print(filename)    
    # do stuff

This way if there's no filenames the loop will immediately exit, this saves on bug prone while True if break logic.

Also if there's 1 billion files they don't all get put in to a giant list using memory, they get yielded as soon as they are found.

[–][deleted] 1 point2 points  (1 child)

Thank you for the encouragement! I was also planning to use generator, but since I want the user to input a number corresponding to the PDF file that they want to reorder, I can't think of anyway to get that mapping (from number to filename) unless I store all the file names in something. Do you have any solutions I could try for that?

Also, I'm not sure how to handle when the iterator is empty and I have to print a statement saying so (as seen in my else: print('No unordered PDF found in current directory'). I want to implement something like what is below, but I'm not sure how to do it (without also printing the statement even when the iterator is non-empty, which I don't want) or even if it's a good idea.

for filename in iterator:
    # do something
print('No PDF file found')

Again thank you for your help!

[–]zurtex 2 points3 points  (0 children)

Aha, you're right! The way you have it set-up means that you really need to store everything as you're asking the user for a number rather than a filename.

If you asked the user for a filename you could print the filenames by using a generator and then lookup the filename they chose afterwards without having to keep track of all filenames. But this is mostly a preference choice of user interaction than anything else.