Bluetail
Bluetail

Reputation: 1291

How to rename multiple image files using the folder name as a prefix in python

I have a folder called 'Cat' which contains lots of .jpg files, such as 069.jpg, 208.jpg, 1134.jpg, img_thermal_158.jpg and so on.

I want to rename these as cat_069.jpg, cat_208.jpg, cat_1134.jpg, cat_img_thermal_158.jpg and so on.

how can I achieve this with python? I'm using Jupyter notebooks via Anaconda on a Windows 10 machine to run python.

my attempt at this which does not work

import os

pre = "cat_"
[os.rename(f, pre + str(f)) for f in os.listdir('../Dataset/Train/Cat') 
if f.endswith(".jpg")]

It gives this error,


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_7536\2602524164.py in <cell line: 4>()
      2 
      3 pre = "cat_"
----> 4 [os.rename(f, 'cat_' + str(f)) for f in os.listdir('../Dataset/Train/Cat') 
      5 if (f.endswith(".jpg"))]

~\AppData\Local\Temp\ipykernel_7536\2602524164.py in <listcomp>(.0)
      2 
      3 pre = "cat_"
----> 4 [os.rename(f, 'cat_' + str(f)) for f in os.listdir('../Dataset/Train/Cat') 
      5 if (f.endswith(".jpg"))]

FileNotFoundError: [WinError 2] The system cannot find the file specified: '069.jpg' -> 'cat_069.jpg'

I don't know what's wrong because it is definitely the right path. I have also tried the absolute path, "C:\Users\me\Jupiter_Notebooks\Dataset\Train\Cat" with the same error.

[os.rename(f, "cat_" + str(f)) for f in os.listdir('C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat') if f.endswith(".jpg")]

FileNotFoundError                         Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_7536\980149228.py in <cell line: 1>()
----> 1 [os.rename(f, "cat_" + str(f)) for f in os.listdir('C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat') if f.endswith(".jpg")]

~\AppData\Local\Temp\ipykernel_7536\980149228.py in <listcomp>(.0)
----> 1 [os.rename(f, "cat_" + str(f)) for f in os.listdir('C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat') if f.endswith(".jpg")]

FileNotFoundError: [WinError 2] The system cannot find the file specified: '069.jpg' -> 'cat_069.jpg'

I have also tried below which produces a similar error.

import shutil
import os
newdir="C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat\\"
for f in (os.listdir('C:\\Users\\me\\Jupiter_Notebooks\\Dataset\\Train\\Cat\\')):
    if f[-4:]==(".jpg"):

        if os.path.isdir(newdir):
            shutil.copy(f,newdir+"/"+"Cat_"+f)
        else:
            os.mkdir(newdir)
            shutil.copy(f,newdir+"/"+"Cat_"+f)

it is

cwd = os.getcwd()
cwd
'C:\\Users\\me\\Jupiter_Notebooks\\Dataset'

# my default os.listdir() is
os.listdir()

['.ipynb_checkpoints',
 'Test',
 'Thermal_image_notebook.ipynb',
 'Train'...

Can someone tell me what I need to change in my code please? thank you.

P.S. here are the files, https://github.com/bluetail14/Thermal_data_images_project

Upvotes: 0

Views: 164

Answers (1)

DYZ
DYZ

Reputation: 57135

The easiest solution, in my view, is to change the cwd to the directory that has the files, rename the files, and change the cwd back if necessary:

cwd = os.getcwd() # remember the old one
os.chdir('../Dataset/Train/Cat')

pre = "cat_"
[os.rename(f, pre + f) for f in os.listdir('.') if f.endswith(".jpg")]

os.chdir(cwd)

Upvotes: 1

Related Questions