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
Post a Comment