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
#13
(Dec-22-2019, 11:13 PM)lastyle Wrote: But as this is a work in Progress i didnt notice earlier that there may exist Folders like bestfileV2.0 which contain e.g. Updatev2.0.exe which belongs to bestfile but doesnt match the Filename. So i need to cover that aswell sadly
Can find folder that match to and also make so it use absolute path with root,
then it easier to see what's going on.
Can fit this in many ways,test different method then you learn how this work.
Example:
import os
import re

path = r'E:\div_code\read\lars'
pat = re.compile(r".*bestfile.*")
for root, dirs, files in os.walk(path):
    for file in files:
        if pat.match(file):
            print(f'{root}\{file}')
    for d in dirs:
        if pat.match(d):           
            folder_pth = fr'{root}\{d}'
            print(folder_pth)
            for f in os.scandir(folder_pth):
                print(f) # If match folder show it DirEntry
                print(fr'{root}\{f.name}') # Make path to this file
Now it will mark DirEntry if it match a folder that contain bestfile.
Output:
E:\div_code\read\lars\bestfile.txt E:\div_code\read\lars\bestfiledoc.txt E:\div_code\read\lars\bestfilexxx.pdf E:\div_code\read\lars\bestfile_pic.jpg E:\div_code\read\lars\catbestfile.txt E:\div_code\read\lars\bestfilefolder <DirEntry 'Updatev2.0.exe'> E:\div_code\read\lars\bestfilefolder\Updatev2.0.exe E:\div_code\read\lars\new\bestfile999.txt
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-23-2019, 03:59 PM

Forum Jump:

User Panel Messages

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