In [2]:
import os 
In [12]:
#Files Present in Directory
os.listdir('/home/kushagra/Desktop/test')
Out[12]:
['##Mercury-SolarSystem-3.mp4',
 '##Jupiter-SolarSystem-9.mp4',
 '##Earth-SolarSystem-1.mp4',
 '##Saturn-SolarSystem-10.mp4',
 '##Mars-SolarSystem-4.mp4',
 '##Venus-SolarSystem-2.mp4',
 '##Uranus-SolarSystem-8.mp4',
 '##Neptune-SolarSystem-7.mp4',
 '##Sun-SolarSystem-5.mp4',
 '##Moon-SolarSystem-6.mp4']
In [23]:
#Spliting name and extension of file
for f in os.listdir():
    print(os.path.splitext(f))
   
('##Mercury-SolarSystem-3', '.mp4')
('##Jupiter-SolarSystem-9', '.mp4')
('##Earth-SolarSystem-1', '.mp4')
('##Saturn-SolarSystem-10', '.mp4')
('##Mars-SolarSystem-4', '.mp4')
('##Venus-SolarSystem-2', '.mp4')
('##Uranus-SolarSystem-8', '.mp4')
('##Neptune-SolarSystem-7', '.mp4')
('##Sun-SolarSystem-5', '.mp4')
('##Moon-SolarSystem-6', '.mp4')
In [24]:
#Printing Name and ext of files
for f in os.listdir():
    name,ext = os.path.splitext(f)
    print(name)
    print(ext)
##Mercury-SolarSystem-3
.mp4
##Jupiter-SolarSystem-9
.mp4
##Earth-SolarSystem-1
.mp4
##Saturn-SolarSystem-10
.mp4
##Mars-SolarSystem-4
.mp4
##Venus-SolarSystem-2
.mp4
##Uranus-SolarSystem-8
.mp4
##Neptune-SolarSystem-7
.mp4
##Sun-SolarSystem-5
.mp4
##Moon-SolarSystem-6
.mp4
In [25]:
#Spliting name on the basis of "-"
for f in os.listdir():
    name,ext = os.path.splitext(f)
    song,artist,number=name.split("-")
    song=song[2:]
    print(song)
Mercury
Jupiter
Earth
Saturn
Mars
Venus
Uranus
Neptune
Sun
Moon
In [31]:
#Renaming the files as per our requirement, using zfill we make sure that every number is of 2 digit.
for f in os.listdir():
    name,ext=os.path.splitext(f)
    song,artist,number=name.split("-")
    song=song[2:]
    number=number.zfill(2)
    os.rename(f,"{0}-{1}{2}".format(number,song,ext))
In [32]:
#You can see name is changed 
os.listdir()
Out[32]:
['05-Sun.mp4',
 '07-Neptune.mp4',
 '09-Jupiter.mp4',
 '03-Mercury.mp4',
 '06-Moon.mp4',
 '01-Earth.mp4',
 '02-Venus.mp4',
 '04-Mars.mp4',
 '10-Saturn.mp4',
 '08-Uranus.mp4']
In [ ]: