2
votes

I have a lot of folders. in each folder, there is an image. I want to rename all of the images with the name of its folder.

For example: Folder "1" has an image "273.jpg" I want to change it into the "1.jpg" and save it in another directory.

This is what I have done so far:

import os
import pathlib

root = "Q:/1_Projekte/2980/"

for path, subdirs, files in os.walk(root):
    for name in files:
        print (pathlib.PurePath(path, name))
        os.rename(os.path.join(path, name), os.path.join(path, os.path.basename(path)))
        print(os.path.basename(path))

The problem is that it works just for the first folder, then it jumps out with the error:

this file is already available...

the tree of folders and the images are like this:

Q:/1_Projekte/2980/1/43425.jpg

Q:/1_Projekte/2980/2/43465.jpg

Q:/1_Projekte/2980/3/43483.jpg

Q:/1_Projekte/2980/4/43499.jpg

So there is just one file in each directory!

2
You are getting the error because when the program tries to rename the second file as 1.jpg it will fail with the error file is already avaialbleRakesh
As @Rakesh pointed out you can't have to files with the name and extension in the same directory.Nazim Kerimbekov
This would happen when your starting postulation is wrong: you say "in each folder, there is an image" but in fact there is more than one image. (Or rather, any type of file – there is nothing in your code that limits it on "just" images.)Jongware
@ Rakesh - there is just only one file in each directory. If it is not the case, do you know any method that changes the name into 1(1).jpg or something like that automatically, or I have to write this code in a classic way.Mohamad Reza Salehi Sadaghiani
@ usr2564301 - I am sure that there is just one image in the subdirectory. I cannot find why the code try to rename it again the same file.Mohamad Reza Salehi Sadaghiani

2 Answers

2
votes

Probably you have some hidden files in those directories. Check it out. If those files are not jpg, you can use following code:

for path, subdirs, files in os.walk(root):
    for name in files:
        extension = name.split(".")[-1].lower()
        if extension != "jpg":
            continue
        os.rename(os.path.join(path, name), os.path.join(path, os.path.basename(path) + "." + extension))
        print(os.path.basename(path))

This code extracts the extension of the file and checks if it is equal to the jpg. If file extension is not jpg, so continue statement will run and next file will check. If file type is jpg, script renames it. Also this code adds original file extension to the new name. Previous code didn't handle that. I hope this helpe you.

1
votes

Maybe this might help...

import os

root = "Q:/1_Projekte/2980/"
subdirs = [x for x in os.listdir(root) if os.path.isdir(x)]

for dir_name in subdirs:
    dir_path = root + dir_name + '/'
    files = os.listdir(dir_path)

    print(dir_name)
    print(files)

    for i in files:
        counter = 1
        extension = i.split('.')[-1]
        new_name = dir_name + '.' + extension

        while True:
            try:
                os.rename(dir_path + i, dir_path + new_name)
                break

            except:
                # If the file exists...
                new_name = dir_name + '({})'.format(counter) + '.' + extension
                counter += 1

This code ensures that even if a file with the existing name happens to exist, it will be suffixed with a number in brackets.