Python Forum

Full Version: if a string has a digit - print
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Greetings and happy Friday!
I'm scanning Network Shares and collecting file names if they have a digit in the name.
I can get the list of all File names but cannot filter (print out) only the ones with the Digit/Digits in the name.
And that is what I need.
Here is part of the code I have a problem with:

import re
import os

ech = 'C:/Users/sys/Desktop/01/'

for root,dirs, files in os.walk(ech):
    #print (files)
    for fls in files :
        print (fls)
        m = re.search(r'\d', fls)
        print ("Matched - ",m)
I'd like to print the whole file name but I managed to print this instead:
"Matched - <re.Match object; span=(10, 11), match='1'>" Sad

Thank you.
You already know the filename is fls. You don't have to print the match object (or the components of it).

if m:
    print(f"Found digit in {fls}")
Thank you bowlofred!
I also found this is working too:
            if re.findall('\d', fls):
                fls=fls.strip()
                print ("MATCHED --- ",fls)