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
#1
Hi all.

I am looking for a solution that allows me to search inside a Folder for a Filename that can look like "*bestfile* or bestfile2* regardless the file extension.

The available sources I found for using walk with file.endswith didn't help me there.

Is there anybody who can guide me into the right direction for python 3.8?
Reply
#2
What about glob.glob() ?
Reply
#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
#4
This seems to work for anything that starts with bestfile which is Atleast more than I have at the moment, but not for files that have bestfile in middle of the name.

How or better what to adjust to archieve this then
Reply
#5
(Dec-20-2019, 05:29 PM)lastyle Wrote: but not for files that have bestfile in middle of the name.

How or better what to adjust to archieve this then
As i mention you can adjust regex for all kind of matches,maybe you should practice a little on regex.
Here a link to regex 101 which is a fine tool for testing/learning.
Now it match all bestfile no matter whats's before or behind.
Reply
#6
Ah okay, got it now and also works. Os.scandir is limited to the actual path. for parsing the sub and subsub folders i need to switch to dirwalk, correct ?
Reply
#7
(Dec-20-2019, 10:08 PM)lastyle Wrote: for parsing the sub and subsub folders i need to switch to dirwalk, correct ?
If use an other walk name os.walk().
import os
import re

for root, dirs, files in os.walk('.'):
    for file in files:
        if re.match(r".*bestfile.*", file):
            print(file)
Reply
#8
Yay , thats great.

Just one more thing (sorry, i didnt notice that i may need this earlier)

lets assume there is a directory that matches my searchpattern, how do i reflect that ?
Reply
#9
(Dec-22-2019, 12:06 AM)lastyle Wrote: lets assume there is a directory that matches my searchpattern, how do i reflect that ?
In the last code with os.walk() it only search for files(in all sub-folders).
So if a folder it's named folderbestfile it will not be in result,
but files with bestfile names in this folder will be in result as wanted.

If need in a other cases to check if something is a file or folder can use.
os.path.isfile(path)
os.path.isdir(path)
Or if using pathlib.
my_file.is_file()
my_file.is_dir()
Reply
#10
But os.Path.Isdir returns the complete path. So I need to check if the return value if it contains bestfile.
Reply


Forum Jump:

User Panel Messages

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