Here is a simple way to create 10 files in Python 3.6+, named from file.txt01
to file.txt10
:
from pathlib import Path
for i in range(1, 11):
f = Path(f'file.txt{i:02d}')
f.write_text('111\n222\n')
If you want to create a new file on every run, sequentially numbered ad infinitum, do this:
from pathlib import Path
i = 1
while True:
if Path(f'file.txt{i}').exists():
i += 1
else:
Path(f'file.txt{i}').write_text('111\n222\n')
break
But that is very inefficient though.
So maybe this is a better solution:
from pathlib import Path
source = Path('/home/accdias/temp')
prefix = 'file.txt'
slots = set([int(_.name.replace(prefix, '')) for _ in source.glob(f'{prefix}*')])
slot = min(set(range(1, max(slots, default=1) + 1)) - slots, default=max(slots, default=1) + 1)
filename = source / f'{prefix}{slot}'
filename.write_text('111\n222\n')
The solution above is nice because it take into account any gaps that may exist and pick the next lowest slot number available.