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
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)– jsbueno