Similar to Sven's solution, but before the full generator:
from itertools import count def gen_filenames(prefix, suffix, places=3): """Generate sequential filenames with the format <prefix><index><suffix> The index field is padded with leading zeroes to the specified number of places """ pattern = "{}{{:0{}d}}{}".format(prefix, places, suffix) for i in count(1): yield pattern.format(i) >>> g = gen_filenames("data_", ".dat") >>> for x in range(3): ... print(next(g)) ... data_001.dat data_002.dat data_003.dat
source share