Monica Heddneck
Monica Heddneck

Reputation: 3125

Search a directory, including all subdirectories that may or may not exist, for a file in Python.

I want to search a directory, including all subdirectories that may or may not exist, for a file in Python.

I see lots of examples where the directory we are peeking into is known, such as:

os.path.exists(/dir1/myfile.pdf)

...but what if the file I want is located in some arbitrary subdirectory that I don't already know exists or not? For example, the above snippet could never find a file here:

/dir1/dir2/dir3/.../dir20/myfile.pdf

and could clearly never generalize without explicitly running that line 20 times, once for each directory.

I suppose I'm looking for a recursive search, where I don't know the exact structure of the filesystem (if I said that right).

Upvotes: 0

Views: 1398

Answers (2)

joel goldstick
joel goldstick

Reputation: 4483

Here is code do find a file (in my case "wsgi.py") below the pwd

    import os
    for root, dirs, files in os.walk('.'):
       if "wsgi.py" in files:
           print root


./jg18/blog/blog
./goat/superlists/superlists
./jcg_blog/jcg_blog
./joelgoldstick.com.16/blog/blog
./blankdj19/blank/blank
./cp/cpblog/cpblog
./baseball/baseball_stats/baseball_stats
./zipcodes/zipcodes/zipcodes
./django.1.6.tut/mysite/mysite
./bits/bits/bits

If the file exists only in one dir, it will list one directory

Upvotes: 0

aghast
aghast

Reputation: 15300

As suggested by @idjaw, try os.walk() like so:

import os
import os.path

for (dir,subdirs,files) in os.walk('/dir1'):
    # Don't go into the CVS subdir!
    if 'CVS' in subdirs:
        subdirs.remove('CVS')

    if 'myfile.pdf' in files:
        print("Found:", os.path.join(dir, 'myfile.pdf'))

Upvotes: 1

Related Questions