Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm pretty new to Python. I'm trying to write a script and as I want to automate it as much as possible, I'm wondering is it possible to set the workspace as a variable so that when it's run as a standalone script or as a tool in ArcToolbox that it will allow everything else to run?

Below for example would it be possible to get the user to select a workspace and then for the script to pick up the shapefiles from this workspace and also create the new geodatabase there?

# Create file Geodatabase
# Import ArcGIS modules
import arcpy
print arcpy.ProductInfo()
# Check out the ArcGIS Spatial Analyst Extension
arcpy.CheckOutExtension("spatial")
# Need to be able to OverWrite Outputs
arcpy.env.overwriteOutput = True
# Set workspace
arcpy.env.workspace = "C:/Prog_Data"
# Set up variables
out_folder_path = "C:/Prog_Data"
out_name = "Prog.gdb"
# Execute CreateFileGDB
arcpy.CreateFileGDB_management(out_folder_path, out_name)
# Set local variables before importing shapefiles to Geodatabase
inFeatures =  ["Points.shp", "Extent.shp", "Rivers.shp", "Population.shp"]
outLocation = "Prog.gdb"
# Execute shapefile to Geodatabase
arcpy.FeatureClassToGeodatabase_conversion(inFeatures, outLocation)

As I said, I'm new to Python so apologies if this is a stupid question etc.

Thanks

share|improve this question
add comment

2 Answers

Rather than suggest modifications to your script, I am going to recommend that you consider adopting Esri's PythonTemplate which includes this functionality and more.

share|improve this answer
add comment

You could create a script with one input parameter (a folder) and change your code slightly to grab the parameter value. Just save the code as .py and point the script to .py file.enter image description here

# Import module and print info
import os, arcpy
arcpy.AddMessage(arcpy.ProductInfo())

# Check out extension and overwrite outputs
arcpy.CheckOutExtension("spatial")   
arcpy.env.overwriteOutput = True

# Set workspace and declare variables
arcpy.env.workspace = out_folder_path = arcpy.GetParameterAsText(0)    
outLocation = "Prog.gdb"
# Create list of feature classes in workspace
inFeatures =  arcpy.ListFeatureClasses()

# Create FGDB and import shapefiles
arcpy.CreateFileGDB_management(out_folder_path, outLocation)       
arcpy.FeatureClassToGeodatabase_conversion(inFeatures, outLocation)

# Remove _shp if it is present in feature class name
arcpy.env.workspace = os.path.join(out_folder_path, outLocation)
newFC = arcpy.ListFeatureClasses()
[arcpy.Rename_management(fc, fc.replace("_shp", "")) for fc in newFC if "_shp" in fc]
share|improve this answer
 
Thank you, I can't even upvote your answer as I'm too new! I've got this to work, is there anyway I can tell it to not put the '_shp' at the end of hte file name when it converts it over? –  Dunuts Jul 20 '13 at 21:59
 
I'm running 10.1 SP1, and it doesn't append '_shp' to the new feature classes. However, I've edited my code to rename the new feature classes if they have "_shp" in them. –  Paul Jul 20 '13 at 22:38
 
Again, thank you very much, greatly appreciated. –  Dunuts Jul 20 '13 at 23:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.