mak47
mak47

Reputation: 303

How to insert characters within file names to make them the same length?

I'm trying to align terminal output to make visually parsing errors easier. I asked this question which is now answered and closed. I believe the issue I have is to do with how I'm generating the terminal statements.

To address this I'm attempting to change standardised the length of the file names instead of the terminal output.

This is what I'm dealing with:

Search Count: 79 - Searching for: The Flying Brick.png ---------------- 004 keypoints matched - 481
Search Count: 80 - Searching for: The Loud One.png ---------------- 001 keypoints matched - 478
Search Count: 81 - Searching for: theTobyKiDD.png ---------------- 001 keypoints matched - 474
Search Count: 82 - Searching for: Thoraner.png ---------------- 000 keypoints matched - 441
Search Count: 83 - Searching for: TiMe2BuRn.png ---------------- 002 keypoints matched - 479
Search Count: 84 - Searching for: Ty Pod Challenge (3).png ---------------- 010 keypoints matched - 416

This is what I want:

Search Count: 79 - Searching for: The Flying Brick.png ------------ 004 keypoints matched - 481
Search Count: 80 - Searching for: The Loud One.png ---------------- 001 keypoints matched - 478
Search Count: 81 - Searching for: theTobyKiDD.png ----------------- 001 keypoints matched - 474
Search Count: 82 - Searching for: Thoraner.png -------------------- 000 keypoints matched - 441
Search Count: 83 - Searching for: TiMe2BuRn.png ------------------- 002 keypoints matched - 479
Search Count: 84 - Searching for: Ty Pod Challenge (3).png -------- 010 keypoints matched - 416

The test cases in the answered question all work, but I can't seem to get it work in my actual code. I'm trying a different approach.

import glob

pad_images = glob.glob(r"C:\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 = longest_file_name.split("\\")[-1] 
longest_int = len(longest)

for f in split_images:
    parts = f.split('.')
    new_part1 = '_' * (24-len(parts[0])) + parts[0]
    new_part = '.'.join([parts[0], new_part1])
    print(new_part)

I'm getting:

`010000111._______________010000111`

I want:

`010000111_______________.png` 

How do I fix this?

Upvotes: 1

Views: 48

Answers (2)

Matt Hall
Matt Hall

Reputation: 8142

In your specific questioon, parts[1] should be png, unless I misunderstood.

More generally, I recommend getting familiar with:

For example, let's look at your example. It's a tiny bit awkard to put a space in after the string and before the fill, but this:

fname, ext = 'The Flying Brick', 'png'
print(fname.ljust(32, '_') + '.' + ext)

results in The Flying Brick________________.png, or you can achieve something similar with an f-string:

print(f'{fname:_<32}.{ext}')

Upvotes: 2

mak47
mak47

Reputation: 303

import glob

pad_images = glob.glob(r"C:\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 = longest_file_name.split("\\")[-1] 
longest_int = len(longest)

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

Some examples from the folder in question:

Atomic Samurai__________.png
BabyYodatheBased________.png
Baradum_________________.png
bcav____________________.png
Beltze__________________.png
BeltzeBubi______________.png
ChihuahuaPaws___________.png
Cptn Cnnbz______________.png

Upvotes: 0

Related Questions