How to make a GIS inventory?
This works for me, using the arcpy.da.Walk
function at ArcGIS 10.1 SP1:
import arcpy, csv, os
workspace = r"c:\GISData"
output = r"C:\temp\test.csv"
with open(output, 'wb') as csvfile:
csvwriter = csv.writer(csvfile)
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace):
for filename in filenames:
desc = arcpy.Describe(os.path.join(dirpath, filename))
csvwriter.writerow([desc.catalogPath, desc.name, desc.dataType])
The csv
module is also used to simplify writing the output file. Excel can open CSV files so you can view them as spreadsheets.
See the arcpy.Describe
function for additional properties you can include in the output.
If you are specifically looking to parse out information from the actual metadata, see the script in this answer: Creating a table containing all filenames (and possibly metadata) in a File Geodatabase
When you use Python, you must use the correct modules to do what you want. To find all files in a directory with extension shp, for example, there are much simpler solutions that was presented without the break, which is awful...(as the solution presented by Nathan W, but there are many, many others, just search on Internet)
Some examples with relevant modules:
1) with the glob module:
shapefiles only:
import glob
import os
os.chdir("mydir")
for files in glob.glob("*.shp"):
print files
shapefiles and geodatabases:
import glob
types = ('*.shp', '*.gbd') # the tuple of file types
files_grabbed = []
for files in types:
files_grabbed.extend(glob.glob(files)) #files_grabbed = the list of shp and gbd files
if you want to search also in the subdirectories:
import glob
for f in glob.iglob("/mydir/*/*.shp"): #search immediate subdirectories
print f
2) with os.listdir and list comprehension (in two lines) -> list of results
path = 'mydir'
shape_files = [f for f in os.listdir(path) if f.endswith('.shp')]
gdb_files = [f for f in os.listdir(path) if f.endswith('.gdb')]
3) with fnmatch module:
import fnmatch
for file in os.listdir('path'):
if fnmatch.fnmatch(file, '*.shp'):
print file
and many others solutions, recursive etc.
Thanks artwork21 and Nathan W for your response. And yes Nathen's code made the magic.
import os, arcpy
#create blank text file
with open("C:\\Temp\\GISlayers.txt", "w") as txt:
for root, dirs, files in os.walk("C:\\Temp\\temp"):
for f in files:
#look for shapefiles
if f.endswith('.shp'):
desc = arcpy.Describe(root + "\\" + f)
#write info to text file
txt.write(desc.name + "," + desc.catalogPath + "\n")
#look for file geodatabases
if f.endswith('.gdb'):
desc = arcpy.Describe(root)
for child in desc.children:
#write info to text file
txt.write(child.name + "," + child.path + "\n")
#look for layer files
if f.endswith('.lyr'):
desc = arcpy.Describe(root + "\\" + f)
#write info to text file
txt.write(desc.name + "," + desc.catalogPath + "\n")
#look for img file
if f.endswith('.img'):
desc = arcpy.Describe(root + "\\" + f)
#write info to text file
txt.write(desc.name + "," + desc.catalogPath + "\n")
Only file name, and location. The pc i'll be working with has lots of coverage (the arc-info file) file, will it work on them too?