Rename files with Python by dynamically adding numbering

Asked

Viewed 1,272 times

0

I have a directory with many pdf lists, and 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))

as can be seen in f_id, there will always be a repeated 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 runs every time a new file is added to a folder. (would be activated with the incron, Linux) then, on a day-to-day basis, there will be some files that already have a numbering and others that should still have the numbering added (always 1 more than the highest value already present in another file). Logically, if there is already a file with numbering, it should be ignored in renaming.

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

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

1 answer

1


Hello,

follows a proposal for solution to your problem.

To facilitate mainly the file appointment 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 that when the id_num is equal to 0, the file is new and does not yet have 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())

Note 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. Also, if there is deletion of any file with id less than the highest id, a new file added will not be contemplated with that missing id in the sequence but with a number more than the highest id present.

  • 1

    Tip - take a look at Python’s pathlib - the above code can be much simpler and more consistent. It’s also easier to avoid using chdir that can’t be trusted. https://docs.python.org/3/library/pathlib.html (ignore the first section in the documentation - and go straight to "Basic Use" - the first part gets diluted about "which class to use to manipulate a Windows path if your program is on Linux" and is only useful on very sophisticated systems)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.