ryitesh
ryitesh

Reputation: 9

Python :Sort filenames in a directory and add a number in the beginning of each filename

1-intro-to deepleanring and computer visionk.MKV
2.Kaggle Deep Learning  - YouTube.MP4
Convolutional Neural Networks - Fun and Easy Machine Learning - YouTube.MP4
Convolutional Neural Networks - The Math of Intelligence (Week 4)YouTube.MKV
Introduction to Deep Learning- What Are Convolutional Neural Networks- YouTube.MP4
Kaggle Deep Learning 3 - YouTube.MP4
Kaggle Deep Learning 4 - YouTube.MP4
Kaggle Deep Learning 5 Data Augmentation - YouTube.MP4
Kaggle Deep Learning 6 - YouTube.MP4
Kaggle Deep Learning 7.mp4
Kaggle Deep Learning 8 - YouTube.MP4

These are the files that needs to be sorted .A filename that contains a number(like 3 in this case) Kaggle Deep Learning 3 -YouTube.MP4 should be renamed as 3 Kaggle Deep Learning - YouTube.MP4. A file name that doesn't contain any number or a file which contains a number in the beginning need not to be renamed.

I have written the following code till now and i am stuck

for f in os.listdir():
    filename,extension = os.path.splitext(f))

In short i want these files to look like this in my directory..

1-intro-to deepleanring and computer visionk.MKV
2 Kaggle Deep Learning  - YouTube.MP4
3 Kaggle Deep Learning  - YouTube.MP4
4 Kaggle Deep Learning  - YouTube.MP4
5 Kaggle Deep Learning  Data Augmentation - YouTube.MP4
6 Kaggle Deep Learning  - YouTube.MP4
7 Kaggle Deep Learning .mp4
8 Kaggle Deep Learning  - YouTube.MP4
Convolutional Neural Networks - Fun and Easy Machine Learning - YouTube.MP4
Convolutional Neural Networks - The Math of Intelligence (Week 4)YouTube.MKV
Introduction to Deep Learning- What Are Convolutional Neural Networks- 
YouTube.MP4

Upvotes: 1

Views: 183

Answers (2)

ryitesh
ryitesh

Reputation: 9

Finally after some effort i have solved this problem,this code has solved my problem to the large extent.

Splitting every string(filename) into a character is the key

import os
dirs = os.listdir(".")

for f in dirs:
filename, extension = os.path.splitext(f)

#splitting every string into a char because we don't know the location of the number
filenames = list(filename)
for char in filenames:
    if(char.isdigit()):
        #print('digit is', char)
        filenames.remove(char)
        filenames.insert(0,char)


        name = ''.join(filenames)

        new_name = '{}{}'.format(name,extension)
        #print(new_name)
        os.rename(f,new_name)

Upvotes: 0

fazkan
fazkan

Reputation: 352

This is not a clean example but it might help you out, run this inside the folder in which you want to rename the files.

CAUTION: Do take a backup of your content before running the code...

import os

dirs = os.listdir(".")

#I am splitting every string into a chr because we dont know the location of the number
split_dir= [list(f) for f in dirs]


print "split_dir", split_dir

y = split_dir

for index, x in enumerate(split_dir):
    for char in x:
        print "char", char
        if(char.isdigit()):
            print "inside is digit"

            y[index].remove(char)
            y[index].insert(0,char)


y = [''.join(x) for x in y]
print "split_dir", y


#this section just renames the files in the current working directory
for index,file_name in enumerate(dirs):
    print("yindex", y[index])
    os.rename(file_name, y[index])

Upvotes: 1

Related Questions