0

Say I have these files

/home/user/one/two/abc.txt
/home/user/one/three/def.txt
/home/user/one/four/ghi.txt

I'm trying to find ghi.txt recursively using the pathlib module. I tried:

p = '/home/user/'
f = Path(p).rglob(*i.txt)

but the only way I can get the filename is by using a list comprehension:

file = [str(i) for i in f]

which actually only works once. Re-running the command above returns an empty list.

I decided to learn pathlib because apparently it's what is recommended by the community, but isn't:

file = glob.glob(os.path.join(p,'**/*i.txt'),recursive=True) 

much more straightforward?

HappyPy
  • 9,839
  • 13
  • 46
  • 68
  • So apparently you have found two ways to solve your problem of which you prefer one. What is your question? – mkrieger1 Jan 09 '20 at 20:15
  • It only works once presumably because `rglob()` returns a generator which gets exhausted by your comprehension. `f = tuple(Path(p).rglob(*i.txt))` would let you loop over it multiple times if you need to do that for some reason – berardig Jan 09 '20 at 20:32

1 Answers1

0

You already have a solution.

Not sure if I read the requirements correctly but I have posted the answer with the following assumptions.

  • PathListPrefix is the starting path beneath which you want to search all files. In your case it might be '/home/user'
  • FileName is the name of the file that you are looking for. In your case it is 'ghi.txt'
  • You are not expecting more than one match.
  • Something other than pathlib modules has to be tried.

As far as straightforward solution is concerned I am not sure about that either. However the below solution is what I could think of using os module.

import os
PathListPrefix = '/home/user/'
FileName = 'ghi.txt'

def Search(StartingPath, FileNameToSearch):
    root, ext = os.path.splitext(StartingPath)
    FileName = os.path.split(root)[1]
    if FileName+ext == FileNameToSearch:
        return True
    return False

for root, dirs, files in os.walk(PathListPrefix):
    Path = root
    for eachfile in files:
        SearchPath = os.path.join(Path,eachfile)
        Found = Search(SearchPath, FileName)
        if Found:
            print(Path, FileName)
            break

The code is definitely having many many more lines than yours. So it is not as compact as yours.

Amit
  • 2,018
  • 1
  • 8
  • 12