python - Need the path for particular files using os.walk() -


i'm trying perform geoprocessing. task locate shapefiles within directory, , find full path name shapefile within directory. can name of shapefile, don't know how full path name shapefile.

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

os.walk gives path directory first value in loop, use os.path.join() create full filename:

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

i renamed path in loop dirpath not conflict path variable passing os.walk().

note not need test if result of .endswith() == true; if you, == true part entirely redundant.

you can use .extend() , generator expression make above code little more compact:

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

or 1 list comprehension:

shpfiles = [os.path.join(d, x)             d, dirs, files in os.walk(path)             x in files if x.endswith(".shp")] 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -