Python Forum

Full Version: Rename files in a folder named using windows explorer
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,

Many of us know we can rename multiple files with windows explorer following the steps here:

https://support.microsoft.com/en-us/topic/how-to-rename-multiple-files-in-windows-xp-with-windows-explorer-e0a7499f-6cd2-5147-3ce6-7748b6dd0dcb#:~:text=Select%20multiple%20files%20in%20a,name%2C%20and%20then%20press%20ENTER.

I have a folder that already contains many files named using the above way.

Now I need to delete some files in that folder. Instead of repeating the steps again manually, can I have a Python code to help me automatically update the names? For example, if there are 3 files named as: XYZ (1), XYZ (2) and XYZ (3). If XYZ (1) is deleted, I want the remaining two files to be renamed as XYZ (1) and XYZ (2) which reflects a new continuous order.
You can rename files using the Python module os. Just look up os.rename().

You probably should first check whether or not a file with the new name exists.

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile(new_name):
    print("The file already exists")
else:
    # Rename the file
    os.rename(old_name, new_name)
The next part for you is trickier. I would think you should first delete all the files you don't want.

Then you can locate all file names containing XYZ( like below.

I have a lot of files that have the format correctAnswers_Week1.txt

import glob
path2files = '/home/pedro/summer2023/21BE/correctAnswersCW/correctAnswers_Week*'
myfiles = glob.glob(path2files)
for f in myfiles:
    print(f)
Let's say you have a file XYZ(2).txt as the first remaining XYZ file you could change the name like this

f = 'XYZ(2).txt'
part_name = f.split('(')
new_name = part_name + '(' + str(count) + ').txt'
os.rename(f, new_name)
Then you can use a loop change the names.

myfiles.sort()
count = 1
for f in myfiles:
    part_name = f.split('(')
    new_name = part_name + '(' + str(count) + ').txt'
    os.rename(f, new_name)
    count +=1
BUT, if your XYZ files have numbers greater that 9 the order will be wrong, because XYZ(10).txt will come before XYZ(2).txt

You could avoid that by counting the length of the string name.

If f = 'XYZ(2).txt' then len(f) = 10 so skip the file if len(f) > 10

Change the longer file names later in the loop.

But, if you don't have file names with numbers > 9, no problem.

I'm sure the experts here know better ways to do this, but this may give you some ideas!
Hi Pedroski55, many thanks for your help. The folder contains files with various names instead of XYZ :) So the loop may need to go through each file name to check.

I am not familiar with Python - not sure how this interesting task can be accomplished in Python.
What do you want the program to do? Should the program delete a selected file and rename any associated files? Should the program rename any files that end with (X) to remove any gaps in X? Do you want to provide a pattern it should use when searching for files (python resequence.py "XYZ.txt") or should it look at all files in a folder?