Question: Env: Python 3.6, O/S: Windows 10

I have the following code that will search for filenames that contain a string either at the start (`.startswith`) of a filename or the end of a filename (`.endswith`), including sub directories and is case sensitive, i.e. `searchText = 'guess'` as opposed to `searchText = 'Guess'`.

I would like to modify`if FILE.startswith(searchText):` that allows a search anywhere in the filename and is case insensitive. Is this possible?

For example, a directory contains two files called `GuessMyNumber.py` and `guessTheNumber.py`.
I would like to search for `'my'` and the code to return the filename `GuessMyNumber.py`

```
#!/usr/bin/env python3
import os

#  set text to search for 
searchText = 'Guess'

#   the root (top of tree hierarchy) to search, remember to change \ to / for Windows
TOP = 'C:/works'

found = 0

for root, dirs, files in os.walk(TOP, topdown=True, onerror=None, followlinks=True):

        for FILE in files:
            if FILE.startswith(searchText):
                print ("\nFile {} exists..... \t\t{}".format(FILE, os.path.join(root)))
                found += 1

            else:
                pass

print('\n File containing \'{}\' found {} times'.format(searchText, found))

```

Thanks guys,
Tommy.

Answer: 
A simple `glob`-based approach:

```
#!/usr/bin/env python3
import os
import glob

#  set text to search for 
searchText = 'Guess'

#   the root (top of tree hierarchy) to search, remember to change \ to / for Windows
TOP = 'C:/works'

found = 0
for filename in glob.iglob(os.path.join(TOP, '**', f'*{searchText}*'), recursive=True):
    print ("\nFile {} exists..... \t\t{}".format(filename, os.path.dirname(filename)))
    found += 1

print('\n File containing \'{}\' found {} times'.format(searchText, found))

```

---

A simple `fnmatch`-based approach:

```
#!/usr/bin/env python3
import os
import fnmatch

#  set text to search for 
searchText = 'Guess'

#   the root (top of tree hierarchy) to search, remember to change \ to / for Windows
TOP = 'C:/works'

found = 0
for root, dirnames, filenames in os.walk(TOP, topdown=True, onerror=None, followlinks=True):
    for filename in filenames:
        if fnmatch.fnmatch(filename, f'*{searchText}*'):
            print ("\nFile {} exists..... \t\t{}".format(filename, os.path.join(root)))
            found += 1

print('\n File containing \'{}\' found {} times'.format(searchText, found))

```

---

You could also use a PERL-compatible (more general) regular expression supported by `re` instead of the POSIX-compatible (less general) supported by `glob` and `fnmatch`.
However, in this simple scenario, the POSIX-compatible is more than enough.
Instead you can locally store all the .py files in your directory with their absolute path, use a simple regex to see if the absolute path has "Guess" or "guess" (depends on your use). Try this code and let us know

```
import pathlib
import os
import re
required=[]
    for filepath in pathlib.Path('C:\\your\\directory').glob('**/*.py'): #any file extension
        required.append(os.path.abspath(filepath)      
nameregex=re.compile(r'(.*Guess)')
mo=list(filter(nameregex.match, required))
print(mo) 
#or len will give you the length of the list
print(len(mo))

```