Zip File Extraction Function

  • Share this:

Code introduction


This function is used to extract a zipfile to a specified path. If the file is corrupted or does not exist, it will print an error message.


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 zipfile.BadZipFile:
        print("This file is not a zip file or it is corrupted.")
    except FileNotFoundError:
        print("The file was not found.")
    except Exception as e:
        print(f"An error occurred: {e}")                
              
Tags: