Rename files with Python, adding numbering dynamically



  • I have a directory with many pdf lists, and I want to rename the files dynamically.

    Ex:

    ~/teste/
    

    |__ProjMed-Química-Físico.Química-quimica_propriedades_coligativas_exercicios.pdf

    |__ProjMed-Química-Físico.Química-quimica_termoquimica_entalpia_lei_de_hess_energia_de_ligacao_exercicios_gabarito.pdf

    So far, my code is like this:

    import os
    os.chdir('/home/matheus/teste/')
    for f in os.listdir():
    f_name, f_ext = os.path.splitext(f)
    f_id, f_subject, f_frente, f_topic = f_name.split('-')
    f_name1 = '{}-{}-{}-{}{}'.format(f_id, f_subject, f_frente, f_topic, f_ext)
    print(f_name1)

    print(sorted(f_name1, key=str))

    how one can notice in f_id, there will always be a repeat identifier. in the case of the example, 'ProjMed'.

    I would like to count how many ProjMed appear (total files with this identifier) and rename them so that it gets something like this:

    1-ProjMed

    2-ProjMed

    3-ProjMed

    etc.

    I am trying to make a script that surrounds every time a new file is added in a folder. (it would be activated with the , from Linux) then, on day to day, there will be some files that already have a numbering and others that must still have the numbering added (always 1 to more than the highest value already present in another file). Logically, if there is already any file with numbering, it should be ignored in rename.

    how could I modify my current script to achieve these goals?

    • Count how many 'identifiers' appear by folder ('ProjMed', for example)
    • ignore the rename of files that already have numbering
    • rename files without numbering with n+1, being n the largest numbering present in a file of this folder



  • 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.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2