My advice is to simply rename increasingly, as putting the date as suffix/prefix (eg dd-mm-yyy_hh:mm:ss) in a cycle like this example would risk many files with equal names (would be changed in the same second or even millisecond):
As a comment I realized that maybe you have to run this over and over again, so you need to get the last one n
of the last file changed to rename the first one as (n+1)_foo.pdf
:
import os
original_dir = 'caminho/para/pasta/original'
files = os.listdir(original_dir) # todos os ficheiros
last_num = max(int(i.split('_', 1)[0]) for i in files if '_' in i) # ultimo numero
no_changed_files = (i for i in files if '_' not in i) # ficheiros ainda nao mudados
for n, file_name in enumerate(no_changed_files, last_num + 1): # renomear os que ainda nao foram
full_path = os.path.join(original_dir, file_name)
if (os.path.isfile(full_path)): # se for ficheiro
os.rename(full_path, '{}/{}_{}'.format(original_dir, n, file_name))
would replace this file_name with the filename between quotes?
– guilherme
file_name is already the file name @Guilherme
– Miguel
It didn’t work, I’m looking for the.Name a way, using if or for
– guilherme
Didn’t work? some error @Uilherme? It would just change in this example the original folders and destination
– Miguel
says that src is not declared
– guilherme
Bug my @Uilherme, sorry, fixed
– Miguel
Opa now changed the name, thank you very much, sorry to abuse, but if I repeat the action he changes the names again and puts and is 1_1_test for example, would not add the 1_ if you already have?
– guilherme
Done @William
– Miguel
+1 for the effort rs, very good Miguel
– Clayton Tosatti
@Guilherme is made to cover this situation, I’m checking if "_" already exists in the
filename
– Miguel
was very good, thank you
– guilherme