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
#11
(Dec-22-2019, 12:01 PM)lastyle Wrote: But os.Path.Isdir returns the complete path
No it return True or False,it dos a test if it's a folder and that folder exits.
>>> os.path.isdir(r'E:\div_code\pan')
True
>>> os.path.isdir(r'E:\div_code\pan999')
False
(Dec-22-2019, 12:01 PM)lastyle Wrote: So I need to check if the return value if it contains bestfile.
This i don't understand,in code dos regex .*bestfile.* the check,and only return files that match this regex.
Reply
#12
(Dec-22-2019, 02:29 PM)snippsat Wrote:
(Dec-22-2019, 12:01 PM)lastyle Wrote: But os.Path.Isdir returns the complete path
No it return True or False,it dos a test if it's a folder and that folder exits.
>>> os.path.isdir(r'E:\div_code\pan')
True
>>> os.path.isdir(r'E:\div_code\pan999')
False
(Dec-22-2019, 12:01 PM)lastyle Wrote: So I need to check if the return value if it contains bestfile.
This i don't understand,in code dos regex .*bestfile.* the check,and only return files that match this regex.

Sure, finding Files works like a charm. 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
Reply
#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
#14
(Dec-29-2019, 06:45 PM)GATYRASI Wrote: The available sources I found for using walk with file.endswith didn't help me there.
Any point of this post?
This has nothing to do with file.endswith this code is for pattern search in filename/foldername regardless of what the file extinctions is.
Reply


Forum Jump:

User Panel Messages

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