Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

PREVIOUS NEXT
Code Example
Python :: how to define function in python 
Python :: python 3 custom sort with compare 
Python :: how to create dictionary between two columns in python 
Python :: pandas read excel with two headers 
Python :: openai python 
Python :: how to execute bash commands in python script 
Python :: one line if statement without else 
Python :: get column pandas 
Python :: python file directory 
Python :: run django localhost server 
Python :: sqlite3 python 
Python :: ping from python 
Python :: get dictionary value python 
Python :: how to use cos in python 
Python :: django collectstatic 
Python :: headless chrome python 
Python :: get the name of a current script in python 
Python :: pandas dataframe column based on another column 
Python :: droping Duplicates 
Python :: python input code 
Python :: python write text file on the next line 
Python :: run matlab code in python 
Python :: seconds to datetime.time 
Python :: change marker border color plotly 
Python :: if name 
Python :: python add to list 
Python :: import get user model django 
Python :: sort a dictionary 
Python :: flask decoding base 64 image 
Python :: find the highest id in model django 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =