Check for #example.txt# in Zip File

  • Share this:

Code introduction


Check if a specified zip file contains a file named 'example.txt'.


Technology Stack : os, zipfile

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zip_file(file_path):
    import zipfile
    import os

    def is_file_in_zip(zip_path, file_name):
        with zipfile.ZipFile(zip_path, 'r') as zip_ref:
            return file_name in zip_ref.namelist()

    if not os.path.exists(file_path):
        raise FileNotFoundError("The file does not exist.")

    if not zipfile.is_zipfile(file_path):
        raise zipfile.BadZipFile("The file is not a valid zip file.")

    return is_file_in_zip(file_path, 'example.txt')                
              
Tags: