Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python zip extract directory

import zipfile

archive = zipfile.ZipFile('archive.zip')

for file in archive.namelist():
    if file.startswith('foo/'):
        archive.extract(file, 'destination_path')
Comment

python extract zip file without directory structure

import os
import shutil
import zipfile

my_dir = r"D:Download"
my_zip = r"D:Downloadmy_file.zip"

with zipfile.ZipFile(my_zip) as zip_file:
    for member in zip_file.namelist():
        filename = os.path.basename(member)
        # skip directories
        if not filename:
            continue

        # copy file (taken from zipfile's extract)
        source = zip_file.open(member)
        target = open(os.path.join(my_dir, filename), "wb")
        with source, target:
            shutil.copyfileobj(source, target)
Comment

python extract zip file

from zipfile import ZipFile
import zipfile

myzip = ZipFile('test.zip') 
myzip.extract(member='Roughwork/pretify.html') 
Comment

python extract all zip files from a directory

import zipfile, os
working_directory = 'my_directory'
os.chdir(working_directory)

for file in os.listdir(working_directory):   # get the list of files
    if zipfile.is_zipfile(file): # if it is a zipfile, extract it
        with zipfile.ZipFile(file) as item: # treat the file as a zip
           item.extractall()  # extract it in the working directory
Comment

python extract 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 can I install XGBoost package in python on Windows 
Python :: localhost server in Python 
Python :: numpy array equal 
Python :: converting month number to month name python 
Python :: how to read a text file from url in python 
Python :: Import CSV Files into R Using fread() method 
Python :: selenium assert text on page python 
Python :: error urllib request no attribute 
Python :: Flatten List in Python Using List Comprehension 
Python :: pandas rename multiple columns 
Python :: pandas add two string columns 
Python :: python pil to greyscale 
Python :: call a Python range() using range(start, stop, step) 
Python :: python check folder 
Python :: ignoring warnings 
Python :: time until 2021 
Python :: python list iterate in 1 line 
Python :: python import beautifulsoup 
Python :: pandas check if value in column is in a list 
Python :: how to use ggplot matplotlib 
Python :: run a loop in tkinter 
Python :: pytorch freeze layers 
Python :: python 3.9 features 
Python :: play mp3 file python 
Python :: convert string to list python 
Python :: python tkinter define window size 
Python :: case in python 
Python :: spacy load en 
Python :: how to use one with as statement to open two files python 
Python :: python gui using css 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =