Zip File Extraction Function

  • Share this:

Code introduction


This function is used to unzip files, extracting them from a specified path to a specified directory.


Technology Stack : zipfile

Code Type : File processing

Code Difficulty : Intermediate


                
                    
def zipfile_extract(file_path, extract_to_dir):
    import zipfile
    try:
        with zipfile.ZipFile(file_path, 'r') as zip_ref:
            zip_ref.extractall(extract_to_dir)
        return True
    except zipfile.BadZipFile:
        return "The file is not a zip file or it is corrupted."
    except FileNotFoundError:
        return "The file was not found."
    except Exception as e:
        return str(e)                
              
Tags: