for root, dirs, files in os.walk(path):
    for file in files:
        if file.endswith('.gz'):
            if file not in processed_files:
                pff.write('%s\n' % file)
How to process only new (unprocessed) files in linux