Extract Files from Zipfile to Directory

  • Share this:

Code introduction


This function is used to extract files from a zipfile to a specified directory. If the provided file is not a valid zipfile, an exception will be raised.


Technology Stack : zipfile

Code Type : File processing

Code Difficulty : Intermediate


                
                    
def zipfile_extract(file_path, extract_to):
    import zipfile

    def is_zipfile(path):
        return zipfile.is_zipfile(path)

    if is_zipfile(file_path):
        with zipfile.ZipFile(file_path, 'r') as zip_ref:
            zip_ref.extractall(extract_to)
    else:
        raise ValueError("The provided file is not a valid zipfile.")                
              
Tags: