Hi.follows a solution proposal for your problem.To facilitate mainly the appointment of the file was made the following class:class fileDataModel:
def __init__(self, id_name, subject, frente, topic, ext, id_num = 0):
self.id_num = id_num
self.id_name = id_name
self.subject = subject
self.frente = frente
self.topic = topic
self.ext = ext
def __repr__(self):
return self.getFileName()
def getFileName(self):
if self.id_num != 0:
return "{}-{}-{}-{}-{}{}".format(self.id_num, self.id_name, self.subject, self.frente, self.topic, self.ext)
return "{}-{}-{}-{}{}".format(self.id_name, self.subject, self.frente, self.topic, self.ext)
Note that the method getFileName returns different strings depending on the value of id_num. It was considered when id_num is equal to 0, the file is new and still has no numbering.Now follows a proposal for automatic numbering of new files:import os
os.chdir('/home/matheus/teste/')
files = dict()
for f in os.listdir():
f_name, f_ext = os.path.splitext(f)
f_name = f_name.split('-')
f_id_num = 0
if len(f_name) == 4: # Arquivo sem identificador
f_id, f_subject, f_frente, f_topic = f_name
elif len(f_name) == 5: # Arquivo ja com identificador
f_id_num, f_id, f_subject, f_frente, f_topic = f_name
f_id_num = int(f_id_num)
else:
print(f"Arquivo com nome fora do padrao: {f}")
continue
newFileDataModel = fileDataModel(f_id, f_subject, f_frente, f_topic, f_ext, f_id_num)
if f_id not in files.keys():
files[f_id] = {'last_id' : f_id_num, 'files' : [newFileDataModel]}
else:
files[f_id]['files'].append(newFileDataModel)
if f_id_num > files[f_id]['last_id']:
files[f_id]['last_id'] = f_id_num
for key in files.keys():
last_id = files[key]['last_id']
for file in files[key]['files']:
if file.id_num != 0:
continue
last_id += 1
old_file_name = file.getFileName()
file.id_num = last_id
os.rename(old_file_name, file.getFileName())
It is noteworthy that the above code always considers the highest id present in the added files. That is, if the highest id is 4, a new file will have id equal to 5. In addition, if there is deletion of any file with id lower than the largest id, a new file added will not be contemplated with this missing id in the sequence and yes with a number more than the higher id present.