Python 3 Script to Compress All Files in a Folder and Save it as ZIP File Using zipfile Library
217 views
Jun 3, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
so in this video I will show you a
0:04
Python script which will actually
0:05
compress all the files in inside a
0:08
directory and create a zip file so I
0:12
basically written this Python script
0:13
here you will see that we have a bunch
0:15
of files inside this folder which is the
0:18
files folder it contains images it
0:20
contains videos as well so I will
0:23
basically compress everything which is
0:26
present inside this folder and create a
0:27
zipped file so if I just execute this
0:30
Python script here in the terminal just
0:32
type here Python app py so what you will
0:36
see in the left hand side it is creating
0:38
this uh compressed zip file all the
0:43
files have been compressed into a
0:45
compressed output.zip file so it created
0:47
this file here output file here so
0:51
everything is compressed inside this
0:53
folder that was present if you see
0:56
videos and images was present original
1:00
size were 221 MGB and the output size of
1:03
this zip file is 184 mAB so in this easy
1:06
way you can compress uh large files
1:09
which are present inside a uh single
1:12
folder you can compress all the files
1:14
and create a zip file right here so for
1:17
this we are using a free package inside
1:19
Python which is a zip file so you simply
1:23
need to install
1:26
this so this is essentially this package
1:32
here you simply install this pip install
1:35
zip file or pip install zip file 2 so I
1:40
think it comes uh within the default
1:43
installation of Python which is zip file
1:45
you need not have to install it so now
1:47
just create a simple app py file and
1:50
right here you just need to import the
1:52
necessary packages import operating
1:54
system and import zip file after that we
1:58
will define a function here which will
2:00
compress the entire
2:02
folder into a
2:04
zip zip file and here we just need to
2:07
provide the folder path and the output
2:11
zip path so inside this this function
2:14
here we will use this module here zip
2:16
file and it contains this function here
2:20
a series of functions you can use any of
2:22
these functions so I just want to use
2:24
this zip file function and inside this
2:27
we specifically provide the arguments
2:30
the output zip f of path here and be uh
2:34
reading it in write mode and just using
2:38
this constant zip
2:40
file
2:42
zip file
2:44
dot
2:45
zip deflated so you can use any of these
2:48
constants to actually create a zip file
2:51
and then we will be just be creating
2:54
it and then we will basically looping
2:57
through all the files which are present
2:59
in the inside the
3:07
directory so we'll be walking through
3:10
the directory here which is passed
3:12
folder path looping through each file
3:15
for file and files
3:18
so so whatever is the file path we will
3:22
join this by the operating system path
3:25
join
3:28
method and then we will
3:32
simply use this function file
3:42
path so after that we will be uh writing
3:45
the zipped file so we simply say zip dot
3:48
write and we're writing this file here
3:51
at that location file
3:54
path and the arc
3:57
name so after that we can simply print
4:01
out that all your files have been
4:11
successfully all
4:13
files are
4:16
compressed so this is essentially this
4:18
function right here and now we just need
4:21
to call this function
4:24
so whatever is the folder that you want
4:27
to compress you just need to give it the
4:31
name so I already have a folder right
4:33
here if you see the
4:35
files folder where all the files are
4:38
present so I will simply directly pass
4:40
the path here the name of the folder
4:42
after that I will simply provide the
4:45
name of the zip file that needs to be
4:47
created so it will be created in the
4:49
same directory which is compress
4:53
output.zip and then we will simply call
4:55
this folder which is compress folder to
4:57
zip and then pass this
5:02
folder to compress and output zip file
5:07
uh that's all that we need to do output
5:10
zipped
5:12
file so this is essentially the script
5:14
here and uh if I execute this now you
5:17
will see on the left hand side it is
5:19
creating the zip file so based upon how
5:22
many files are present it can take
5:23
longer so now you will see all files are
5:26
successfully compressed and it has
5:28
created this zip file so you can
5:31
actually extract the zip file here so
5:35
various softes are there winner so you
5:37
can extract the
5:39
files and then it will extract all the
5:42
files so it will create this folder
5:44
compressed output which contains the
5:46
original files so in this easy way you
5:48
can use this Python script the link is
5:50
given in the description to get all the
5:52
source code so this will save you a lot
5:55
of time to compress the files into a zip
5:58
file so thank you very much for watching
6:00
this video and also check out my website
6:03
freemediattoolsh.com
6:05
uh which contains thousands of tools
#Programming
#Scripting Languages