Python Forum
Assign image names in folder to images in other folder.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assign image names in folder to images in other folder.
#8
A example.
import os

path = r'E:\div_code\rename'
new_path = r'E:\div_code\new'
os.chdir(path)
for index,img in sorted(enumerate(os.listdir(path), 1)):
    if img.endswith('.png'):
        #print(index, img)
        os.rename(img, f'{new_path}/{index:03}.png')
See that i change to path where images are os.chdir(path),if not has to use os.path.join() in rename.
String formatting used f-string from 3.6-->.
>>> n = 5
>>> print(f'{n:03}')
005
>>> print(f'{n:08}')
00000005
>>> print(f'{n:015}')
000000000000005
Reply


Messages In This Thread
RE: Assign image names in folder to images in other folder. - by snippsat - Sep-01-2018, 05:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  No Script Folder in Python 3.6.2 Download jriemer 6 17,779 Aug-21-2023, 02:34 PM
Last Post: SaWe1995
  Insert images in a folder into dataframe tofi 0 5,477 Dec-14-2018, 08:05 PM
Last Post: tofi
  Batch processing and saving into new folder aeritano 3 5,082 Jun-10-2018, 01:17 AM
Last Post: aeritano
  Backtesting a folder with csv files, problem fiddelush 8 4,900 Mar-13-2018, 11:57 AM
Last Post: fiddelush

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020