today i had to download a lot of files for my work purpose but after downloading the files
i saw that the files are not sorted serially i mean it should be sorted serially like 1 2 3 4 and
5. but here in this folder you can see that the files are not sorted serially so after go through
the first file i have to scroll down a bunch of file for finding the second file i mean this is
so annoying and it can happen with any one of you suppose you have a bunch of files in a folder and
you have to go through each files serially and the files are not sorted so for avoiding the problem
you can actually go through all of these files and rename the files to be sorted serially one by one
but this will be so time consuming and can be a lot of errors that's why i have decided to write
a simple python script to rename all of these files and sort these files serially one by one
and in this python project we will automate our python to do some real life works
and in a few moments i will show you how to do that but before we get started if you guys are
new in my channel then please please subscribe like this video and press the bell icon for the
future videos and if you guys have any question about this video you guys can comment down
and also let me know what kinds of project you want to see from me in my next tutorial
that's it guys let's get started now open your favorite code editor and then create a new project
and inside this project create a new python file so from here i have to select python file and
here i have to give a name for this python file so my python file name will be main dot py that's
it now after creating a new python file we have to import os it means operating system
you don't need to install this os separately because this is a built-in package from python
now after importing os we have to go to that file where our files are placed so that's why we have
to just call our os and then we have to add child directory and here we have to give the path file
address that's why if i go to my folder here you can see that this is the files now we have
to actually copy this folder address from here so for copying this folder address just click on that
section here you can see that now everything is selected now copy this from here and then
go to our code and then paste it here now after adding the folder address now we have to check out
this program is actually currently accessing this path file address or not so for checking this out
right print and here we have to call our os and then we have to add get cwd that's it now if we
run the program now here you can see that we have an error here in line two and this error
is actually happening because of our this slash here now if you face these problems you just have
to replace this slash with our forward slash so now i am just replacing the slashes here and it
will be forward slash this one also this one this one and this one also now if we run this again
here you can see that our output is exactly as same as our given file path so it means
our program is working now after this one i want to see what we have inside this folder okay so
for checking this out we just have to write f4 and then let's take a variable and it will be f
so and it will be in os dot then we have to check our list directory that's it
and then here we have to just print our f that's it guys now let's run this again
and here you can see that now we have all the file names here
now i want to rename these files but i don't want to rename the file extension here
so in our file here you can see that this is our file name
and this is our extension now i want to remove the extension i don't want to rename this extension
so for doing this first we have to split this file name into two separate parts one will be our file
name and the second one will be our extension okay so for doing this we just have to remove this one
and then here we have to print os and then we have to call our path file and then we have to
split text here we have to split our this text and i want to split our f that's it now let's
run this program and here you can see that we have now separated our files into two parts in
our first poll we have our this file name and in our second tuple we have our this extension here
now i will do one thing i will actually put these two part into two separate variables
okay so for doing this here we have to take two variable first one will be our f name
and our second variable will be file extension e x t that's it now here you can see that we
have saved our this two part into our this two variable now i want to only see our this file
name without our extension so for doing this we just have to print and we have to print our
f underscore it means we just want to see our file name now if i run this program
here you can see that we only have our file names but we don't have any extension here
after doing this here you can see that we have three part
one is our course name second one is our topic's name and the third one is our context number
okay and this three part is actually separated by this dash here
this is perfect now i want to split this whole name into three separate part one is our python
course one is our file topic name and one is our context number so for doing this here you can see
that we have our file name now we just have to split this file name into three different parts
by the help of our this dash so that's why here i am writing split and here i want to split the file
names with our dash that's it if i now run this program and here you can see that our one file
name is actually now divided into three parts one is our course name one is our topic's name and one
is our context number this is really amazing guys now my task is so easy i just have to put this
context number at the first here first we have to actually save these three files into three
different variables so that we can actually swap the variables and print the output
okay so for doing this here instead of print now i want to take three variables first one will be our
python course so that's why it will be f underscore course and then i want to save
the title name so that's why let's take another variable and it will be f underscore title and
then i want to save the context number as well so that's why it will be f underscore number
that's it guys now we have saved our file names into these three different variables
now i just have to print the output according to my preference so first i want to actually
place the numbers so that's why we have to print and then here i will add our formatted string
so that i can add multiple variables i can add multiple sections so here first i will save our
context number and then i will give a dash then i will replace our title number then i will give
another dash and after our title number i want to replace our course name then i want to replace our
file extension now we have to define the variables also here so our first one is for our file number
so that's why it will be file number then we have to replace our file title so that's why it will
be file title then we have to replace our file codes then we have to replace our file extension
so that's why it will be f ext that's it now if i run this program and let's see our output
so here you can see that first we have our numbers then we have our topic names
it means our titles then we have our course name and we have our text extension but before this
title we have something here and i want to remove this thing so that's why i will add format here so
dot format that's it and inside this parenthesis we have to replace these variables
let's say now if i run this again now here we consider we have only our this title here
but here we can see that in our title we have some weird spaces here
one is here and another one is here and another one is here so i want to remove these spaces
so for removing this space we have to copy these three variables from here and then paste it here
and here we have to actually set these variables as in independent variables so that's why we have
to call our this variable again and it will be course and then we have to actually strip away
our these spaces so that's why we have to call our strip that's it now our course section is
done now we have to do the same for our this title that's it and here it will be f title and then we
have to paste it here also and this one will be f number that's it guys now if i run this again
and guys here you can see that we don't have any quad space here now after these things i want to
actually sort the numbers here here you can see that five is at the first and then we have our
10 at the center and then we have our one here now i want to sort these things into ascending
order it means one two three four five and so on we can do this by the help of our digit number
it means here you can see that we have 10 files here and for this files we have only one digit
but for this one we have two digit here so now we have to actually compare the digit
numbers so that we can sort these things so for sorting this one first i have to add a zero here
before hour five it means zero five zero three zero six zero two and so on
so for adding a zero at the first we have to
and i am opening this section from our first letter that's it now if i run this program again
now here you can see that we don't have any star here before our numbers now i want to add a 0
before our 5 and the output will be 0 5 0 3 0 6 0 2 and so on so for doing this we have to here
add another property which is dot and it will be z field and here we have to give the number it means
how many zeros or how many digits i want to put before our this digit numbers and i want a 0 here
before the single digit so that's why it will be 2 that's it now if i run this program
and here you can see that now we have 0 here before our single digit numbers we have 0 5 0
3 0 6 0 t and 1 0 because this is already a double digit so that's why we don't have to add
0 before it so we have done this thing so we have actually renamed our all of these files now we
have to save these files in our this folder so for doing this let's go to our code here again
and here i will put this complete title into a variable so let's take a new variable and
it will be new underscore file that's it now we have stored our this complete title name
inside our this variable now after this one now we have to type our final command which is we have to
call our os and then we have to call our rename function and then i want to rename our f here
here you can see that we have our main section f so we have to rename our f and we will rename
our this files according to our new file that's it now before i run this program let's divide this
screen with our output section so now here you can see that we have our output folder here now if i
run this program these files will be converted into our this format now let's run this program
and guys here you can see that all the files are now sorted by their number and it is now
in ascending order one two three four five and six and guys here you can see that we have our
first file and we don't have to go through a bunch of files for reading the second file
and this python script literally saved my life and i hope it will do the same for you
so that's it guys hope you enjoyed this tutorial and if you guys really do then please please
subscribe like this video and press the bell icon for the future videos and if you guys have
any question about this video you guys can comment below also let me know what kinds of tutorial you
want to see from me in my next tutorial that's it guys let's see you in our another tutorial
you