0

I'm trying rename all files which start for a specific word. This works for me if I write the entire name, but not if I put *. I don't find the correct syntax. Any suggestions? Thank you!

os.rename("C:/Users/myname/Downloads/Reference*.xlsx", "C:/Users/myname/Downloads/" + " " + "Lote" + ".xlsx")

I don't know if I need put the "*" in other part.

Old name: files wich start by Reference word. New name for this files: "Lote"

Thank you.

I'm trying with this, but I don't find the solution:

import glob
import os

identificador = 0
ruta_carpeta = 'C:/Users/myname/Downloads'

lista_archivos = sorted(glob.glob(ruta_carpeta + 'Reference/*.xlsx'))

for i in lista_archivos:   
    nuevo_nombre = ruta_carpeta + '/' + "Lote" + '.xlsx'        
    os.rename(i, nuevo_nombre)
  • 2
    wildcards aren't supported by `os.rename`. Either use "ren" command (Windows) or use `glob.glob` and loop. – Jean-François Fabre Mar 21 '23 at 08:48
  • 1
    You should provide the expected new name for a given name so people can help you more efficiently ([edit] your question for that) – Jean-François Fabre Mar 21 '23 at 08:51
  • Probably you could use regex when looping over files to decide whether rename them or not. – Gameplay Mar 21 '23 at 08:53
  • @Jean-FrançoisFabre I got it with your idea, thank you! import os Import glob for name in glob.glob('C:/Users/myname/Downloads/Reference*.xlsx'): nuevo_nombre = 'C:/Users/myname/Downloads' + '/' + "Lote" + '.xlsx' os.rename(name, nuevo_nombre) –  Mar 21 '23 at 09:29

0 Answers0