Zip File Extraction Function

  • Share this:

Code introduction


This function is used to extract all files from a zip file to a specified directory.


Technology Stack : zipfile

Code Type : File operation

Code Difficulty : Intermediate


                
                    
def zipfile_extract(file_path, extract_path):
    import zipfile
    try:
        with zipfile.ZipFile(file_path, 'r') as zip_ref:
            zip_ref.extractall(extract_path)
    except FileNotFoundError:
        print(f"The file {file_path} does not exist.")
    except zipfile.BadZipFile:
        print(f"The file {file_path} is not a zip file or it is corrupted.")                
              
Tags: