Zip File Extraction Function

  • Share this:

Code introduction


This function is used to unzip a zip file to a specified directory. If the file is not a valid zip file, a FileNotFoundError exception will be raised.


Technology Stack : zipfile

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zipfile_extract(source_path, destination_folder):
    import zipfile
    if zipfile.is_zipfile(source_path):
        with zipfile.ZipFile(source_path, 'r') as zip_ref:
            zip_ref.extractall(destination_folder)
    else:
        raise FileNotFoundError("Provided file is not a valid zip file.")                
              
Tags: