Python 3 OS Module Script to Rename All Files in a Folder With Custom Pattern & Name in Terminal
309 views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys welcome to this video So
0:02
in this video I will show you a Python
0:04
script which will actually rename bulk
0:08
files in a folder So now let's suppose
0:11
if you have a folder which contains a
0:13
lot of files which are not named in a
0:16
particular manner So as I'm targeting
0:20
this folder right here as you can see it
0:22
contains a lot of files lot of uh image
0:25
files So I just want to rename these
0:28
files in 1 2 3 order like this So 1 JPG
0:32
2.jpg 3 dot 4 I need to systematically
0:37
rename all these files in a particular
0:39
order So for this I can actually run
0:42
this Python script here So let me just
0:45
change the folder name here to data and
0:48
run this Python script So just now see
0:51
how it will save you a lot of time If I
0:54
just run this Python script just notice
0:56
on the left hand side it will rename all
1:01
your files systematically So you'll
1:03
see it is doing its process in the
1:06
background You can see all these
1:08
thousands of files image files have been
1:11
renamed in a particular manner right
1:12
here 1 JPG 2 JPG 3 4 5 6 7 You can see
1:18
in a systematic particular order right
1:21
here So that's the thing that I'm
1:23
talking about It will save you a lot of
1:25
time because you don't need to manually
1:27
rename all the files This script will
1:29
automatically do this for you So now let
1:31
me show you the Python script which is
1:33
responsible for
1:35
this I have given the script in the
1:38
description of the video So for this you
1:40
just require the operating system module
1:42
and then here you mention whichever
1:44
folder name that you're working with So
1:47
you need to give the path
1:50
here and then you
1:53
simply get the total number of files
1:56
using the list directory function After
1:59
getting the files you simply put which
2:02
order to rename So I'm just renaming it
2:05
to 1 2 3 4 5 6 So I'm starting it with
2:08
one and then we simply using the for
2:11
loop like this for each file we are
2:14
getting the path here by operating
2:17
system path dot join function folder
2:21
path file
2:23
name After that here in this if
2:25
condition we are comparing that if it's
2:28
a
2:29
file then only we need to rename this
2:33
to a new file name So first of all for
2:37
renaming it we also need to extract the
2:39
extension For this we use the
2:42
split split text
2:47
function We split the extension Then we
2:50
put the new file
2:52
name like this Right and then we join
2:56
the full file
2:59
name After that we rename this So this
3:02
contains this function here rename And
3:04
then we rename this particular file path
3:07
with the new file path And then we print
3:10
out the statement right here that
3:14
your rename has been successfully
3:16
completed And then we simply increase
3:18
the value of count by one Every time the
3:23
loop goes to the next round this count
3:27
value is increased So in this easy way
3:30
you can construct this script and
3:32
basically run this to any folder which
3:34
contains a lot of files because it will
3:36
rename this to a particular order If I
3:39
change it from 1 to two So now the file
3:42
name will start from two So now you will
3:46
see it will actually the first file name
3:49
will be renamed to two
4:06
Sorry this is not dash this is extension
4:09
Sorry
4:18
this sorry this is your
4:23
data if I again do this you'll see file
4:26
name already exists so we already did
4:28
that so you can put execute this script
4:31
to the directory where the files are not
4:33
renamed in a particular manner then this
4:36
script will work at the starting I
4:38
showed
4:39
It will save you a lot of time So thank
4:41
you very much for watching this video
4:43
Also check out my website
4:45
freemediatools.com
4:47
uh which contains thousands of tools
#Scripting Languages
#Software