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]
Why not
import glob
?and i get all the jpeg i want, with absolute path
os.walk
gives you the path to the directory as the first value in the loop, just useos.path.join()
to create full filename:I renamed
path
in the loop todirpath
to not conflict with thepath
variable you already were passing toos.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:or even as one list comprehension:
Seems
os.path.abspath(finename)
will work. Please try.