Skip to content

Support `pathlib` in `generator_utils`

I would like to be able to do something like this:

from pathlib import Path

for input_path in Path('.').glob('*.in'):
   print(input_path.read_text())

But Path('.') does not work locally because there the files reside in data/sample and data/secret. So it would be nice if generator_utils supported this, either in the way open is now overridden, or by exposing functions to get a Path object to the given directories (if they exist) or the current directory (if they don't).


The use case for now is that I want to avoid code like this:

    for i in range(1, 13+1):
        with open(f"{i:02}.in", "r") as input_file, open(f"{i:02}.ans", "r") as answer_file:

Instead, I would like to just get an iterator for all files in a directory, and pathlib seems to be the best fit for that. However, for this specific use case, having parameterized tests based on input and output files is probably a better solution. I would be fine with going that route as well; let me know if you want me to create a separate issue for that. :)

Edited by Frank Mulder