Something that I learned as good practice is working with copies of my shapefiles and not the originals. When doing this you do create several temporary tables and/or shapefiles and these need to be cleaned up.
## System arguments # Input Streets shapefile Streets = sys.argv # Output Streets shapefile Streets_output = sys.argv # Output Streets shapefile Streets_output2 = sys.argv # facility streets layer facilstr = sys.argv # Set the workspace for the processing files gp.workspace = os.path.dirname(sys.argv) # Make copies so that processing doesn't take place on the original. gp.AddMessage("\nCreating processing files...") gp.Copy_management(Streets, "Streets_process.shp") gp.Copy_management(facilstr, "facilstreets.shp") gp.AddMessage ("Copies created")
This creates copies of the input shapefiles which I then use for processing. Because there are these temp files, in the end of the script I need to delete those temp files. For this you need to import the “os” and “glob” functions such as import os, glob in the beginning of the script. Then at the end of the script we use these functions to clean up the temp files. I also cleanup the xml files that are created when working with temporary tables.
gp.AddMessage("Cleanup...") # Delete "process" files for file in glob.glob(gp.workspace+"/Streets_process*"): os.remove(file) for file in glob.glob(gp.workspace+"/facilstreets*"): os.remove(file) for file in glob.glob(gp.workspace+"/*.xml"): os.remove(file) gp.AddMessage("\nDone.\n")