Search
 
SCRIPT & CODE EXAMPLE
 

BASIC

make zipfile from directory py

import zipfile
filePaths = [] # Make an array string with all paths to files
for root, directories, files in os.walk("MyDirectoryPath"): # Scans for all subfolders and files in MyDirectoryPath
        for filename in files: # loops for every file
            filePath = os.path.join(root, filename) # Joins both the directory and the file name
            filePaths.append(filePath) # appends to the array
z = zipfile.ZipFile("MyDirectoryPathWithZipExt.zip", 'w')
with z:
    for file in filePaths: # Loops for all files in array
        z.write(file) # Writes file to MyDirectoryPathWithZipExt.zip
Comment

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 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
Basic :: pmatplotlib draw a square with a magenta dotted line and pentagon markersython matplotlib overlaped 
Basic :: visual basic get mouse position 
Basic :: the terminal process failed to launch 
Basic :: split to arraylist vb.net 
Basic :: robocopy sync one way 
Basic :: how to dynamically change the font size of a button visual basic 
Elixir :: elixir after 
Elixir :: elixir join list of strings 
Elixir :: elixir list map key string to atom 
Elixir :: phoenix ecto preload 
Elixir :: elixir function pattern matching 
Scala :: scala random number 
Scala :: How to declare constant variable in scala 
Scala :: how loop in scala 
Actionscript :: iis appcmd stop site 
Excel :: google sheets convert month to number 
Excel :: excel or 
Perl :: perl remove all whitespace 
Perl :: what happened to perl 6 
Pascal :: for loop pascal 
Powershell :: How to test HDD health in PowerShell 
Clojure :: clojure read file line by line 
Erlang :: get erlang version 
Assembly :: how to open chrome in vbscript 
Assembly :: dd utility seek 
Javascript :: jquery vslidation remove spaces from input 
Javascript :: filesaver.min.js cdn 
Javascript :: ajax cdn 
Javascript :: How to get the browser to navigate to a URL in JavaScript 
Javascript :: how to find number in string js 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =