![]() |
if a string has a digit - print - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: if a string has a digit - print (/thread-32029.html) |
if a string has a digit - print - tester_V - Jan-16-2021 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'>" ![]() Thank you. RE: if a string has a digit - print - bowlofred - Jan-16-2021 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}") RE: if a string has a digit - print - tester_V - Jan-16-2021 Thank you bowlofred! I also found this is working too: if re.findall('\d', fls): fls=fls.strip() print ("MATCHED --- ",fls) |