Unzip Function for Specified Directory

  • Share this:

Code introduction


This function is used to unzip files into a specified directory.


Technology Stack : zipfile, os

Code Type : File operation

Code Difficulty : Intermediate


                
                    
def zipfile_extract(file_path, extract_to_dir):
    import zipfile
    import os

    def extract(zip_path, target_dir):
        with zipfile.ZipFile(zip_path, 'r') as zip_ref:
            zip_ref.extractall(target_dir)

    if not os.path.exists(extract_to_dir):
        os.makedirs(extract_to_dir)
    extract(file_path, extract_to_dir)                
              
Tags: