Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python: create zipfile

import os
import zipfile
    
def zipdir(path, ziph):
    # ziph is zipfile handle
    for root, dirs, files in os.walk(path):
        for file in files:
            ziph.write(os.path.join(root, file), 
                       os.path.relpath(os.path.join(root, file), 
                                       os.path.join(path, '..')))
      
zipf = zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED)
zipdir('tmp/', zipf)
zipf.close()
Comment

python zip files

import shutil
shutil.make_archive(output_filename, 'zip', dir_name)
Comment

python create zip file

import shutil
import zipfile

# base_name is the name of the zip file you want to create
# format is zip for zip file
# root_dir is the direct path of the folder or file you want to zip
shutil.make_archive(base_name='zip_file_name', format='zip', root_dir='data')

# read zip file from current path
with zipfile.ZipFile(file='zip_file_name.zip', mode='r') as zip_ref:
   # create folder name extract_data in current directory with the extracted data
   zip_ref.extractall(path='extract_data')

# Extract a single file from a zip file
with zipfile.ZipFile(file='zip_file_name.zip', mode='r') as zip_ref:
   # Extract a file name called secrets.dat
   zip_ref.extract(member='secrets.dat')
  
 # extract a list of filename within a zip file
with zipfile.ZipFile(file='zip_file_name.zip', mode='r') as zip_obj:
    # Get list of files names in zip
    filenames = zip_obj.namelist()

    # Iterate over the list of file names in given list & print them
    for filename in filenames:
        print(filename)
Comment

PREVIOUS NEXT
Code Example
Python :: how to print an index in python 
Python :: easy python gui 
Python :: leer fichero de texto con columnas como diccionario python 
Python :: Amazing Trees with python turtle 
Python :: getting-the-last-element-of-a-list 
Python :: python triangular number 
Python :: python button graphics.py 
Python :: # add keys to existing dictionary 
Python :: find email address pytho 
Python :: numpy replace all values with another 
Python :: calculate the R^2 for X and Y python 
Python :: python using os module file name from file path 
Python :: how to use with statementin python 2.4 
Python :: how to format a file in python 
Python :: Object of type datetime is not JSON serializable 
Python :: numpy subtract 
Python :: pyplot aera 
Python :: check if a number is in a list python 
Python :: max of empty list python 
Python :: speak by a discord bot in python 
Python :: unpacking tuples in python 
Python :: convert png rgba to rgb pyhton 
Python :: python get function docstring 
Python :: pandas check is field is null or empty 
Python :: how to extract column from numpy array 
Python :: The MEDIA_URL setting must end with a slash. 
Python :: i = 1 while i <= 100: print(i * *") i = i + 1 
Python :: .corr() python 
Python :: python - sending mail 
Python :: SystemError: error return without exception set 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =