May-05-2021, 05:18 AM
any()
takes an iterable and returns true if any member of that iterable is true. Handy in cases where you don't care about the information itself, just that some part is true. But if you want to print the filenames, then print the filenames. Don't use any().
Here's a version with any().
import os dtoscan = 'some\\Dir\\To\\Scan\\' tofind = "latest" filenames = os.listdir(dtoscan) if any(tofind in x for x in filenames): print("At least one filename matches") else print("No filenames matched")If I wanted to print the filenames, I wouldn't bother. I'd do this instead:
import os from pathlib import Path dtoscan = 'some\\Dir\\To\\Scan\\' tofind = "latest" matches = [x for x in os.listdir(dtoscan) if tofind in x] if matches: print("The following files matched:") for file in matches: print(os.path.join(dtoscan,file)) else print("No filenames matched")