Unzip File to Directory Function

  • Share this:

Code introduction


This function is used to unzip files to a specified directory. If the file does not exist or the file is corrupted, an error message will be printed.


Technology Stack : zipfile

Code Type : Function

Code Difficulty : Advanced


                
                    
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)
    except FileNotFoundError:
        print(f"文件 {file_path} 未找到")
    except zipfile.BadZipFile:
        print(f"文件 {file_path} 不是有效的zip文件")                
              
Tags: