joe_riddly
joe_riddly

Reputation: 19

Renaming files in the order they are titled python

I'm using a python script that other guys have helped me with to rename all .jpg or .png files in a directory to whatever I want in order. So if I have 20 .png files in a directory, I want to rename them in order from 1-20.

The script I have DOES this and I'm happy with it. However, it was just pointed out that the files I have been renaming with this script have been out of order.

As an example, when I rename 1.png to testImage1.png, I'm really renaming testImage10.png as testImage1.png. I tested this with my script by creating 5 text files with the same content, but text files 1-3 I put in different content to keep track of what is what after I'm done renaming. Sure enough, everything is mixed up.

import os
import sys

source = sys.argv[1]
files = os.listdir(source)
name = sys.argv[2]

def rename():
    i = 1
    for file in files:
        os.rename(os.path.join(source, file), os.path.join(source, name+str(i)+'.png'))
        i += 1

rename()

I took the time to try and use my (limited) python knowledge to create a series of if/elif statements to sift through and rename the files with the correct name in order.

def roundTwo():
    print('Beginning of the end')
    i = 1
    for root, dirs, files in os.walk(source):
            for file in files:
                    print('Test')
                    if source == 'newFile0.txt' or 'newFile0.png':
                            os.rename(os.path.join(source, file), os.path.join(source, name+str(i)+'.txt'))
                            print('Test1')
                            i += 1
                    elif source == 'newFile1.txt' or 'newFile1.png':
                            os.rename(os.path.join(source, file), os.path.join(source, name+str(i)+'.txt'))
                            print('Test2')
                            i += 1


roundTwo()

I did a fair amount of searching to include using Re or fnmatch but nothing comes exactly close to what I'm looking to do. Perhaps I'm using the wrong terms to search? Any insight helps!

Upvotes: 0

Views: 1583

Answers (2)

TavoGLC
TavoGLC

Reputation: 919

If all the files have some sort of basename you can modify your first function to extract the number assigned to the image

baseName='testImage'
def rename():
    for file in files:
        number=file[len(baseName):file.find('.png')]
        os.rename(os.path.join(source, file), os.path.join(source, name+number+'.png'))

Hope it helps

Upvotes: 0

Jeril
Jeril

Reputation: 8571

If your problem is with 1 and 10, then you can use natural sorting. Sort your variable files as follows:

from natsort import natsorted, ns
natsorted(files, alg=ns.IGNORECASE)

Example:

>>> x = ['a/b/c21.txt', 'a/b/c1.txt', 'a/b/c10.txt', 'a/b/c11.txt', 'a/b/c2.txt']
>>> sorted(x)
['a/b/c1.txt', 'a/b/c10.txt', 'a/b/c11.txt', 'a/b/c2.txt', 'a/b/c21.txt']

>>> natsorted(x, alg=ns.IGNORECASE)
['a/b/c1.txt', 'a/b/c2.txt', 'a/b/c10.txt', 'a/b/c11.txt', 'a/b/c21.txt']

Upvotes: 1

Related Questions