Need the path for particular files using os.walk()
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")]