Feb-17-2022, 12:14 AM
Hello, I'm trying to replace part of a text in a filename for multiple files. But it is not working. Your help is greatly appreciated.
Example:
my document 2022XX_reference.docx
my document 2022XX_manual.docx
my document 2022XX_manual.pdf
Example:
my document 2022XX_reference.docx
my document 2022XX_manual.docx
my document 2022XX_manual.pdf
Output:Output:
my document 202205_reference.docx
my document 202205_manual.docx
my document 202205_manual.pdfimport os
counter = 0
path = r"C:\files\documents"
files = []
for file in os.listdir(path):
if file.endswith(".*"):
if file.find("XX") > -1:
counter = counter + 1
newFileName = file.replace("XX", "05")
files.append((file, newFileName))
