Using python to zip files

preview_player
Показать описание
In this video, we have explained to you that how we can create zip files using python. First of all we have written a program in python to make zip files from existing files, after that we have written a program in python to add directories and multiple paths to our zip file. Pycharm IDE is used in this video.
-------------------------------------------------------------------------------------------------
Code for creating zip file from existing files:
import zipfile
---------------------------------------------------------------------------------------------------
Code for adding directories and multiple paths to our zip file
# importing required modules
from zipfile import ZipFile
import os

def get_all_file_paths(directory):

# initializing empty file paths list
file_paths = []

# crawling through directory and subdirectories
for filename in files:
# join the two strings in order to form the full filepath.

# returning all file paths
return file_paths

def main():
# path to folder which needs to be zipped
directory = './myfolder'

# calling function to get all file paths in the directory
file_paths = get_all_file_paths(directory)

# printing the list of all files to be zipped
print('Following files will be zipped in this program:')
for file_name in file_paths:
print(file_name)

# writing files to a zipfile
# writing each file one by one
for file in file_paths:

print('All files zipped successfully!')

if __name__ == "__main__":
main()

#linuxhint #python
Рекомендации по теме
Комментарии
Автор

hello sir, I hope you are fine I did use the program when it print everything success but nothing in the zipfile

lookingfind
Автор

is there a way to put all files in a folder to a zip?
- thanks in advance

projjectrip
Автор

Hi, can I zip file in the GCS bucket. My file is already in bucket, I need to zip it and again save into GCS bucket. If you have any code sample or your video, please help me. Thanks in advance.

ItsRanaJee
Автор

Hi Sir, I have a zip file i try so much time to unzip if its possible i can send the zip file and plz find and give me the password plz help

Ninja_YT
Автор

What if I want to define the location where to zip?

razinahmed
Автор

How can I add a password to protect my zip files that contains my photos ?

peterkim
Автор

to call main use if __name__=='__main__':

softfussion