In a Python script, I create an output file. I want its name to change accordingly to the its old versions in the output folder. For example:
output_file_name = 'output.txt'
if output.txt already exists --> output_file_name = output_(2).txt
if output_(2).txt already exists --> output_file_name = output_(3).txt
...
My trial:
import os.path
def check_versions(last_version_filename):
# if an output_file_name_(n).txt is present then
# return output_file_name(n+1).txt
#extract the version of the file inside the output folder
try:
old_version = last_version_filename.split('(')[1].split(')')[0]
# case in which no output_(n).txt version are present
except IndexError:
old_version = 1
if old_version == 1:
to_be_replaced = '.txt'
else:
to_be_replaced = '_({}).txt'.format(old_version)
new_version = int(old_version) + 1
last_version_filename = last_version_filename.replace(to_be_replaced,'_({}).txt'.format(new_version))
OUTPUT_FILE_NAME = 'output.txt'
# if output.txt file already exists, then we need to modify its name
if os.path.isfile('./' + OUTPUT_FILE_NAME):
# last_file_version is the name of the last version file in the folder
OUTPUT_FILE_NAME = check_versions(last_version_filename)
So how can I check the last version of the file in a folder? Consider that its format will always be output_(n).txt
. So the greatest n
determines last file version.
In addition, the main fault of this code is that it works fine if
output.txt
output_(2).txt
.
.
.
output_(n).txt
are ALL present. But if output.txt
is missing, the code will create output.txt
as latest version output file. So you will have output.txt
which is newer than output_(2).txt
. And this could result tricky.
Again, it is clear I need to know which is the latest version and then naming the output file accordingly.
How would you solve this?
You could sort the filenames present in the directory that conform to your pattern, then add one to the number in the last one:
import os
import re
valid_filenames = [filename for filename in os.listdir(os.curdir) if filename.startswith('output_')]
n_valid = len(valid_filenames)
if n_valid == 0:
if os.path.isfile('output.txt'):
next_filename = 'output_(2).txt'
else:
next_filename = 'output.txt'
else:
regex = re.compile(r'\((\d+)\)')
tagged_filenames = sorted((int(regex.search(filename).group(1)), filename) for filename in valid_filenames)
next_filename = f'output_({int(tagged_filenames[-1][0]) + 1}).txt'