Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How rename the images in folder

I have 35 folders and each folder contains 50 images, and every image has a different name, like copy(3), rename, youanem and so on. I want to rename all the images in all the folders to sequence numbers, i.e. folder 0 contains 50 images, and it should give them numbers like 0-49 and the same for the rest of the folders and their images. Folder names are 0-9 and then A-Z.

Is this possible to program for it , or i have to do it manually

This is my initial try , i didnt try it completely :

from os import rename, listdir

folder = "D://images//"
fnames = listdir('.')

for fname in fnames:
    if fname.startswith(folder):
        rename(fname, fname.replace(name, '', 1))

I am not getting idea of how to give it name

Thanks

like image 406
Pieter Avatar asked Sep 14 '13 19:09

Pieter


People also ask

How do I rename multiple JPG files at once?

You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.

How do I change the names of files in a folder?

Find and select the file, then select File > Rename. Type the new name and press Enter.

How do I rename all pictures in a folder on Android?

Find the location you want to rename the files on your Android smartphones. Now long-press the files and folders until you see a green tick icon>Do the same and tick all files you want to batch. Next, find the rename button on the top bar and click on it.


1 Answers

The Easiest and Cleanest, Iterate through all the files and rename with index.

import os
os.getcwd()
collection = "C:/darth_vader"
for i, filename in enumerate(os.listdir(collection)):
    os.rename("C:/darth_vader/" + filename, "C:/darth_vader/" + str(i) + ".jpg")
like image 60
Peace Ngara Avatar answered Oct 01 '22 03:10

Peace Ngara