Batch Copy Shapefiles to Geodatabse: How to improve current script

532
2
05-20-2012 05:28 AM
PeterWilson
Occasional Contributor III
Firstly, please note that I'm new to programming and Python. I've created the following Python script to loop through a directory, validate the shapefile names that I then copy to a specified File Geodatabase. The are some shapefiles that have that already have "_shp" attached the shapefile name and the "ValidateTableName" adds "_shp" to the output Feature Class. I have striped out "_shp" before copy the shapefile to the Geodatabase. I have noticed that where the shapefile already had "_shp" it drops an additional character at the end. How to i prevent this from happenning. i.e. citiespop_shp.shp = citispo

import arcpy, os
... arcpy.env.workspace = r"S:\Projects\H108392\H108392.gdb"
... arcpy.env.OverwriteOutput = True
... inWksp = r"S:\Projects\H108392\data_received\KageraMonograph\KAGERA_GIS\_Vector_Data"
... for root, dirs, files in os.walk(inWksp):
...     for name in files:
...         if name.endswith(".shp"):
...             path = os.path.abspath(os.path.join(root, name))
...             base = arcpy.ValidateTableName(os.path.basename(path))
...             print "Processing "+base
...             arcpy.CopyFeatures_management(path, "S:/Projects/H108392/H108392.gdb/"+base.rstrip("_shp"))


Any advice would be appreciated and as well as any pointers wher I could improve the structure of my code.

Regards
Tags (2)
0 Kudos
2 Replies
MarcoBoeringa
MVP Regular Contributor
The error is in the incorrect usage of Python's "rstrip" command. "rstrip" removes ANY occurrences of the characters in the search string you supply (in your case "_shp") from the right side of the processed string. This is why it is going wrong, since "citiespop_shp" contains an additional "p" character as the ending, and since "p", is part of "_shp", it is also removed from "citiespop", resulting in "citiespo". The removing only stops at the "o" character ("citiespo"), as that is the first character not part of the search string "_shp"

Please note that for "rstring", the order of the characters you supply is irrelevant. If you coded:

"teststring.rstring("phs_")"

the results would be exactly the same, e.g. giving you "citiespo" for the processed basename "citiespop_shp"

You will need to use another of Python's string commands to accomplish what you want to do.
0 Kudos
BruceNielsen
Occasional Contributor III
Peter,

I think slicing can help you in this case. base[:-4] should return everything except the last 4 characters.
0 Kudos