How to select specific filenames nested within several folders?
-
I have hundreds of shapefiles that are stored in separate folders, which are nested within several other folders. Among the shapefiles I only need the ones that are named 'POINT*.shp' How do I get these specific shapefiles so I can then add fields, calculate fields, etc.? If there is a better option than os.walk, please tell. I'm only beginning to learn python, so this is very new to me. Thanks!
-
Answer:
Use os.walk and then loop through the files appending to a list, where the filename starts with 'point' and ends with '.shp'. import os path = "D:/Files/GIS/Datafiles/" shp_list = [] for dirpath, dirnames, files in os.walk(path): for f in files: if f.lower().endswith(".shp") and f.lower().startswith("point"): print f fullpath = os.path.join(dirpath, f) shp_list.append(fullpath)
S.S. at Geographic Information Systems Visit the source
Other answers
There is a similar question http://gis.stackexchange.com/questions/59350/how-to-list-feature-classes-of-multiple-geodatabase-in-multiple-folder, but this is only helpful if you have access to ArcMap 10.1 SP1. If so you could use the http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000023000000 to look for shapefiles. If you want any feature class with POINT in the name, you could do the following: import arcpy, os, fnmatch # Set the workspace with user input arcpy.env.workspace = "Your directory here" inWorkspace = arcpy.env.workspace pointList = [] for dirpath, dirnames, filenames in arcpy.da.Walk(inWorkspace, followlinks=True, datatype="FeatureClass"): for filename in filenames: # Add any field with "POINT" in the filename when creating list if fnmatch.fnmatch(filename, "*POINT*"): pointList.append(os.path.join(dirpath, filename)) This creates a list (pointList) of complete paths to shapefiles with "POINT" in the name within your specified directory. Alternatively you could include type="Point" in the arcpy.da.Walk() to search for only POINT feature classes.
Barbarossa
Related Q & A:
- How To Select Odd Numbers In Excel?Best solution by Yahoo! Answers
- How to select all articles and their similar articles from MySQL?Best solution by Stack Overflow
- How to select specific data from listbox?Best solution by stackoverflow.com
- how to select max and min?Best solution by Stack Overflow
- How do I submit my website into several search engines?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.