Python Forum
if a string has a digit - print
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
if a string has a digit - print
#1
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.
Reply
#2
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}")
tester_V likes this post
Reply
#3
Thank you bowlofred!
I also found this is working too:
            if re.findall('\d', fls):
                fls=fls.strip()
                print ("MATCHED --- ",fls)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] a 4-digit display vr34 2 290 Apr-12-2024, 05:38 PM
Last Post: vr34
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 339 Jan-27-2024, 05:23 PM
Last Post: Winfried
  prefix ID Number with 0,00 make 3 digit. mg24 1 742 Oct-06-2022, 07:20 AM
Last Post: ibreeden
  Remove a space between a string and variable in print sie 5 1,777 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Can you print a string variable to printer hammer 2 1,937 Apr-30-2022, 11:48 PM
Last Post: hammer
  Print first day of the week as string in date format MyerzzD 2 2,011 Sep-29-2021, 06:43 AM
Last Post: MyerzzD
  How to print string multiple times on new line ace19887 7 5,722 Sep-30-2020, 02:53 PM
Last Post: buran
  Is this right code for 3 digit pin ? fatjuicypython 1 1,811 Aug-21-2020, 11:15 PM
Last Post: bowlofred
  Print a certain string only the first time it appears in a test file buttercup 5 2,763 Jul-23-2020, 01:30 PM
Last Post: palladium
  How to print string multiple times separated by delimiter Mekala 1 1,892 May-23-2020, 09:21 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020