Search code examples
pythonmaxfilenames

Extract only most recent file based on filename


I want to unzip only the most recent file from files that have been updated. Here's my code:


def main():

    dest_dir = 'C:/Users/PycharmProjects/kpi/flats'
    zip_file = 'Z:/OUT.zip'

    with ZipFile(zip_file) as zipObj:
        for zip_info in zipObj.infolist():
            if zip_info.filename[-1] == '/':
                continue
            if zip_info.filename.startswith('homeware/R2D2/DATA/OUT/Flat/FlatFile_r2d2_01012018'):
                print(zip_info.filename)
                zip_info.filename = os.path.basename(zip_info.filename)
                zipObj.extract(zip_info,dest_dir)

The extracted files are:

homeware/R2D2/DATA/OUT/Flat/Flat_01012018_180216.csv
homeware/R2D2/DATA/OUT/Flat/Flat_01012018_210147.csv
homeware/R2D2/DATA/OUT/Flat/Flat_01012018_230148.csv

But I want only the last one based on the filename and not metadata


Solution

  • So what I did is instead of extracting at each iteration, I appended the files that satisfy the 'if' requirements to a list. Then I extracted the max of that list which is the file I was looking for. Here's the updated code:

    def main():
    
        dest_dir = 'C:/Users/mbelahce040119/PycharmProjects/kpi/flats'
        zip_file = 'Z:/DATA/OUT/COMMON_2018/201810/OUT_201810.zip'
    
        with ZipFile(zip_file) as zipObj:
            files_sat = list()
            for zip_info in zipObj.infolist():
                if zip_info.filename[-1] == '/':
                    continue
                if zip_info.filename.startswith('homeware/R2D2/DATA/OUT/Flat/FlatFile_r2d2_01012018'):
                    print(zip_info.filename)
                    files_sat.append(zip_info.filename)
                    zip_info.filename = os.path.basename(max(files))
            zipObj.extract(max(files_sat), dest_dir)