0

I'm trying to rename and add pad the names of a few hundred files to the same length.

So far I've managed to correctly rename and pad the file names within my IDE but I'm not sure how I link that to actually rename the files themselves.

Atomic Samurai__________.png
BabyYodatheBased________.png
Baradum_________________.png
bcav____________________.png

This is the code that does the rename and the padding within my IDE:

import glob, os

pad_images = glob.glob(r"C:\Users\test\*.png")

split_images = []
for i in pad_images:
    split = i.split("\\")[-1]
    split_images.append(split)

longest_file_name = max(split_images, key=len) 
longest_int = len(longest_file_name)

new_images = []
for i in split_images:
    parts = i.split('.')
    new_name = (parts[0]).ljust(longest_int, '_') + "." + parts[1])

I've been trying to get os.rename(old_name, new_name) to work but I'm not sure where I actually get the old name from as I've split things up into different for loops.

mak47
  • 303
  • 2
  • 14
  • Isn't `i` the old name? – Pranav Hosangadi May 05 '22 at 16:35
  • Yes, and I can change the name of the second i used in the second for loop so I can refer it individually but do I then need to drop the whole thing inside another loop? – mak47 May 05 '22 at 16:41
  • Not sure I understand what the problem is. `i` is the old name. `new_name` is the name you want. Surely renaming it is a simple [`os.rename(i, new_name)`](/q/2491222/843953). If `i` and `new_name` don't contain the full path, then all you need to do is append the name to the directory (which you already know is `r"C:\Users\test"`) – Pranav Hosangadi May 05 '22 at 16:43

1 Answers1

1

Try saving the old file names to a list and do all the modifications (split and rename) in a single loop thereafter:

path = "C:/Users/test"
images = [f for f in os.listdir(path) if f.endswith(".png")]
length = len(max(images, key=len))

for file in images:
    parts = file.split("\\")[-1].split(".")
    new_name = f'{parts[0].ljust(length,"_")}.{parts[1]}'
    os.rename(os.path.join(path,file), os.path.join(path,new_name))
not_speshal
  • 22,093
  • 2
  • 15
  • 30
  • Error on the first line; `NameError: name 'file' is not defined` but that looks a lot neater than what I was trying! – mak47 May 05 '22 at 16:50
  • `FileNotFoundError: [WinError 2] The system cannot find the file specified: '010000111.png' -> '010000111_______________.png'` -- Which is odd because 010000111.png is indeed my first file. – mak47 May 05 '22 at 16:51
  • Hm, I think I can fix this. The split is removing the file path, I assume os.rename needs the file path! Thank you! Much cleaner version! – mak47 May 05 '22 at 16:53
  • You could use `os.join` as in the edit – not_speshal May 05 '22 at 16:53
  • Yeah removing the split didn't actually work but os.join did! I assumed it needed the file path but I guess not? What is os.join doing what leaving the file path in place isn't? – mak47 May 05 '22 at 16:55
  • 1
    `os.join` doesn't do anything special, it basically does `os.path.sep.join(*inputs)`. Skipping the `split` doesn't work because (depending on how you called it) `ljust` doesn't do anything and gives new_name == old_name` – Pranav Hosangadi May 05 '22 at 17:00