Search code examples
pythondataframezip

How to search for specific zip file from the directory?


I have 100's of zip files in the following directory. How to search for the specific zip file from the directory. Search criteria could be 'date' and 'salary' of the zip. For example, file abc_19980821_salary.zip, this file belongs to the date 21-08-1998.

Please suggest how to search a folder like this:

 C:\Users\country
     abc_19980821_salary.zip
     xyz_20200829_salary.zip
     pqr_20050620_salary.zip
     stu_19990815_salary.zip
     klm_20040722_salary.zip

I want to search pqr_20050620_salary.zip:

with zp.ZipFile('C:/Users/country/pqr_20050620_salary.zip') as z:
    with z.open("file.TXT") as f:
        # read the dataset
        df = pd.read_csv(f, delimiter = "\t")
print(df.head())

Solution

  • You can use glob to search for files using wildcards.

    import os
    import glob
    import datetime
    
    top = r"C:\Users\jezequiel\Desktop"
    
    d = datetime.date(1998, 8, 21)
    ds = d.strftime('%Y%m%d')
    pattern = f'*_{ds}_salary.zip' # use wildcards (*)
    
    # make sure we have a match
    with open(f'foobar_{ds}_salary.zip', 'wb') as f:
        pass
    
    print(glob.glob(os.path.join(top, pattern)))