Creating File Listings with Python
Part of many data management projects is keeping a record of what files are where. Often this can involve creating a list in Excel of all files within a folder area. Doing this manually is impractical as, typing out the information takes a long time and of course can also be a demoralising task.
A simple python script that has proved its weight in gold on some recent projects is shown below:
Enter the python code into ArcCatalog or ArcMap to list out all feature classes found in the defined workspace (env.workspace). Note that there is an array of “List” functions noted further in this blog that enables the user to quickly extract files names for rasters (ListRasters), tables (ListTables) and folders (ListFiles) etc.
Press enter to run the script:
The list of feature class names are found in grey below the code. It is possible to export this list directly from python into csv format (by using the csv.writer module) and to Excel (by using the csv.excel class).
Manually Importing into Excel
Select the result from the python window in ArcGIS. Copy the text and paste it into windows notepad. Save the file as a *.txt file.
Open Excel > data > import external data and navigate to where you saved your *.txt file. Import as a CSV as this is how python prints the feature class names.
Feature class names are now added to Excel filling the first row. The user can tidy the file names by using Excel’s find/replace feature.
Normally I want my list to be in a column. To do this I select and copy row1. Then I click in row 2 column A, right-click the cell, go to Paste Special and select Transpose. The row of data should now have been transposed to a column.
The result is that rather than having to manually type each file name into excel a GIS user can quickly export file names into excel using python script.
One of the best things about using Python to perform these kinds of tasks is that a user has flexibility to quickly export a list for a variety of data types. Using python in ArcGIS 10 the programme creatively suggests functions for you as you are typing. Furthermore it is possible to create a process that will search not one folder but all subfolders within a folder.
As I write the word ‘list’ the programme automatically suggests options to me, as shown in the table below:
Adapted from Esri's ArcGIS Desktop Help
The functionality in the list enables a user to search and extract files names for an array of data types, workspaces and versions.
Posted by Simon Kettle, GIS Technician, Exprodat
- ArcGIS Desktop 10 help: Working with sets of data in Python
- Python v2.7.3 documentation: CSV File Reading and Writing
Simon: Just realize that your script is for Arcgis 10 and i´m working on Arcgis 9.3. Thanks anyway.
Posted By: Jaime - 29/05/2012 21:51:15
Hi Jamie thanks for your question. There is a button that has the appearance of a window with a greater than sign ">" at the top of ArcMap and ArcCatalog. Click this and a two pane window will open. You can copy and paste/type in the code directly into the left hand pane. See here for more information: www.help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002100000017000000
Posted By: Simon Kettle - 28/05/2012 22:41:52
Simon: Do you know how to enter the code in arcmap or arccatalog? Thanks
Posted By: Jaime Lopez - 28/05/2012 20:20:05