Python Forum
Search file with *filename* python 3.8
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search file with *filename* python 3.8
#3
glob.glob() as @Gribouillis suggests can work if also mix in fnmatch.
I find it easier to use regex alone with eg os.scandir() when matching pattern in file name and regardless the file extension.

Example.
import os
import re

for file in os.scandir('.'):
     if re.match(r"^bestfile\w+", file.name):
        print(file.name)
Files in folder:
Output:
E:\div_code\read\lars λ ls 9bestfile bestfile2.txt bestfilexxx.pdf file_search.py 'sample_file .txt' bestfile19999.jpg bestfile34444.txt catbestfile.txt la.py
Now running code over in this folder it will only match only bestfile(any combination).
Can now eg change regex for all kind of pattern matches in filename.
Output:
E:\div_code\read\lars λ python file_search.py bestfile19999.jpg bestfile2.txt bestfile34444.txt bestfilexxx.pdf
Reply


Messages In This Thread
Search file with *filename* python 3.8 - by lastyle - Dec-19-2019, 09:07 PM
RE: Search file with *filename* python 3.8 - by snippsat - Dec-20-2019, 05:15 PM

Forum Jump:

User Panel Messages

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