I have a folder contains images. each image named as
abc_001.jpg
abc_002.jpg
I need to rename the images by deleting the first text abc_ and start from the number
I did as
import os
path = '/Desktop/my_folder'
folder = os.fsencode(path)
filenames = []
for file in os.listdir(folder):
filename = os.fsdecode(file)
if filename.endswith ('.jpg'):
filenames=filename.replace('abc_','')
but it doesn't work . how can I replace the first text or deleting them?
i tried
for file in os.listdir(folder):
filename = os.fsdecode(file)
if filename.endswith ('.jpg'):
filenames = filename.replace('abc_', '')
os.rename(filename, filenames)
but got
os.rename(filename, filenames)
FileNotFoundError: [Errno 2] No such file or directory: 'abc_000000000009.jpg' -> '000000000009.jpg'
I solved the problem so anyone who faces the same problem can try it
for file in os.listdir(folder):
filename = os.fsdecode(file)
if filename.endswith ('.jpg'):
filenames = filename.replace('abc_', '')
os.rename(os.path.join(path,filename), os.path.join(path,filenames))