Python Forum

Full Version: filtering files using 'any()"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Greetings!
I'm trying to filter files in a directory using the function 'any()',
The problem is I can't find any good examples on the net about it.
For example, I want to print names of files that do not have the word "latest".
Here is the code I got so far :

import os
from pathlib import Path

dtoscan = 'some\\Dir\\To\\Scan\\'
tofind = "latest"
for fd in os.listdir(dtoscan) :
    dp = os.path.join(dtoscan,fd)

    if os.path.isfile(dp) :
        any(tofind,dp)
I could do this without any() function but I really would like to use it.
Thank you!
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")
EDIT: ninjad by bowlofred Cool

I think that any() documentation is quite clear:

- you need iterable
- you need expression which returns boolean value
- there is 'short-circuit' which breaks out of iteration if first True is encountered
- return value is boolean (True or False)

The way you try to use any is not the intended way.

Example: is there any filename in directory which name contains 'latest':

any('latest' in file_name for file_name in directory)
If first True is encountered then any returns True and stops (this is the point of 'any' - it takes only one to make it True)
This example is taken verbatim from the documentation of os.scandir(). It prints the names of all entries in a directory which are files and which names don't start with a dot.
with os.scandir(path) as it:
    for entry in it:
        if not entry.name.startswith('.') and entry.is_file():
            print(entry.name)
All snippets produce errors. NOne is working.
(May-05-2021, 04:54 PM)tester_V Wrote: [ -> ]All snippets produce errors. NOne is working.

Can you show the error (entire traceback)?
(May-05-2021, 04:54 PM)tester_V Wrote: [ -> ]All snippets produce errors. NOne is working.
You posts code you run and error you get.
Working example filter out so only get image(.png) files.
import os

path = r'E:\div_code\new\cat_pic'
with os.scandir(path) as it:
    for entry in it:
        #print(entry)
        if entry.name.endswith('.png') and entry.is_file():
            print(entry.name)
Output:
cat_1.png cat_2.png cat_3.png cat_4.png
As first test would run this so see that it read all files.
import os

path = r'E:\div_code\new\cat_pic'
with os.scandir(path) as it:
    for entry in it:
        print(entry)
Output:
<DirEntry 'cat_1.png'> <DirEntry 'cat_2.png'> <DirEntry 'cat_3.png'> <DirEntry 'cat_4.png'> <DirEntry 'cities.txt'> <DirEntry 'foo.txt'> <DirEntry 'scapy.txt'>
I see why the snippets not working. They all looking for a filename "latest" but I'm not looking for the 'latest' file name.
I'm looking for a string with the word 'latest' in the files in the directory...
I'd like to use 'any()' to tell if the file has or has not the word.

Sorry for the confusion!
(May-05-2021, 05:07 PM)tester_V Wrote: [ -> ]I see why the snippets not working. They all looking for a filename "latest" but I'm not looking for the 'latest' file name.
I'm looking for a string with the word 'latest' in the files in the directory...
I'd like to use 'any()' to tell if the file has or has not the word.

Sorry for the confusion!

Then for each file, you'll need to open() the file, read() the data and compare that data to your target.

If you get a match, store the filename or just print it out and move on to the next file.

ETA:
And using any() to find the match in the file would be reasonable. Something like...
with open(filename, "r") as f:
    if any(tofind in line for line in f):
        print(f"Filename {filename} matched")
The files are big and many, I do not want to do 'readlines' and if I'll do open each file then it is like doing 'for' loop, and I do not need 'any()'

I thought I can test each file as a whole file without checking each line.

Thank you.
Pages: 1 2