(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 sadlyCan 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 fileNow 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