45

I'm trying to perform some geoprocessing. My task is to locate all shapefiles within a directory, and then find the full path name for that shapefile within the directory. I can get the name of the shapefile, but I don't know how to get the full path name for that shapefile.

shpfiles = []
for path, subdirs, files in os.walk(path):
    for x in files:
        if x.endswith(".shp") == True:
            shpfiles.append[x]
Schack
  • 833
  • 2
  • 7
  • 18

3 Answers3

105

os.walk gives you the path to the directory as the first value in the loop, just use os.path.join() to create full filename:

shpfiles = []
for dirpath, subdirs, files in os.walk(path):
    for x in files:
        if x.endswith(".shp"):
            shpfiles.append(os.path.join(dirpath, x))

I renamed path in the loop to dirpath to not conflict with the path variable you already were passing to os.walk().

Note that you do not need to test if the result of .endswith() == True; if already does that for you, the == True part is entirely redundant.

You can use .extend() and a generator expression to make the above code a little more compact:

shpfiles = []
for dirpath, subdirs, files in os.walk(path):
    shpfiles.extend(os.path.join(dirpath, x) for x in files if x.endswith(".shp"))

or even as one list comprehension:

shpfiles = [os.path.join(d, x)
            for d, dirs, files in os.walk(path)
            for x in files if x.endswith(".shp")]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Just a small addition to the answer. The file tail could be capitalized, eg.: ```test.jpg``` or ```test.JPG```, which would return ```False``` in the statement ```if x.endswith(".jpg"):```. We could use the Python built-in function [```str.lower()```](https://docs.python.org/3.8/library/stdtypes.html#str.lower) to prevent possible _False_ s. With **```if x.lower().endswith(".jpg"):```** we lower the string before the comparison. Thereby, ```test.jpg``` and ```test.JPG``` will return ```True```. – Juan CA Jul 26 '20 at 13:47
0

Seems os.path.abspath(finename) will work. Please try.

shpfiles = []
for path, subdirs, files in os.walk(path):
    for x in files:
        if x.endswith(".shp") == True:
            shpfiles.append(os.path.join(path, x))
gsmaker
  • 533
  • 1
  • 4
  • 21
  • 6
    No it won't. `abspath` will base the filename of the *current working directory*, which can be a totally different path altogether. – Martijn Pieters May 09 '13 at 15:32
0

Why not import glob ?

import glob 

print(glob.glob('F:\OTHERS\PHOTOS\Panama\\mai13*\\*.jpg') )

and i get all the jpeg i want, with absolute path

>>> 
['F:\\OTHERS\\PHOTOS\\Panama\\mai13\\03052013271.jpg', 
'F:\\OTHERS\\PHOTOS\\Panama\\mai13\\05052013272.jpg', 
'F:\\OTHERS\\PHOTOS\\Panama\\mai13\\05052013273.jpg']
kiriloff
  • 25,609
  • 37
  • 148
  • 229
  • 3
    `glob()` only supports a fixed depth of subdirectories, `os.walk()` supports arbitrary depths. See [How can I search sub-folders using glob.glob module in Python?](http://stackoverflow.com/questions/14798220/14798263#14798263) – Martijn Pieters May 09 '13 at 15:47
  • Original question could be interpreted as a single directory. For those reading this answer who don't require subdirectories (like me), glob is more pythonic because it is more concise and yields a more immediately useful result. – michael Jul 25 '18 at 21:35
  • 1
    after 8 years these replies are no longer valid. Glob now supports recursion. – maxadamo Dec 19 '21 at 17:59