[Python] Get a file name from a list of files with multiple conditions

Discussão em 'Python' iniciado por Stack, Julho 25, 2025.

  1. Stack

    Stack Membro Participativo

    I have a list of files in a folder partly with very similar names and with different data types. I would like to find only specific files that always contains some certain strings in its name.

    For example, list of files:

    file_list = ['00AAAredneck20240213134024.csv',
    '00AAAredneck20240213134024.raw',
    '00AAAredneck20240213134024cal.csv',
    '01BBB_CC-PPP20240213134024_merged.csv',
    '01DEFG20240213134024.csv',
    '01DEFG20240213134024.raw',
    '01DE FG20240213134024.csv',
    '01DE FG20240213134024.raw',
    '01DEFG-PPP20240213x001.csv',
    '01DEFG-PPP20240213x002.csv',
    '01DEFG-PPP20240213x003.csv',
    '01DEFG-PPP20240213x004.csv',
    '01DEFG-PPP20240213x005.csv',
    '01DEFG-PPP20240213x006.csv',
    '01DEFG-RAW20240213x001.dat',
    '01DEFG-RAW20240213x002.dat',
    '01DEFG-RAW20240213x003.dat',
    '02HHHLLL20240213134024.csv',
    '02HHHLLL20240213134024.raw',
    '20240213134024Comb.csv',
    'PICsfile_1AAAredneck_20240213134024',
    'PICsindex_1AAAredneck_20240213134024',
    'Log20240213134024.txt',
    'O20240213134024.ini',
    'data.ini',
    ]


    My aim is to find the both files named

    • 01DEFG20240213134024.csv
    • 01DE FG20240213134024.csv

    my code looks complicated in my eyes

    search_file =[x for x in file_list if 'DE' in x and 'FG' in x and 'csv' in x and 'PPP' not in x]
    print(search_file)


    Is there a nicer way to transfer the including and excluding strings than 'string1' in x and 'string2' in x and 'string3' not in x?

    Continue reading...

Compartilhe esta Página