4

I'm trying to write a script and as I want to automate it as much as possible.

Is it possible to set the workspace as a variable so that when it is run as a standalone script, or as a tool in ArcToolbox, that it will allow everything else to run?

Below, for example, I want 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)
6

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. The blog posting has since been removed but I'll leave the link in case it is ever put back. The code for Esri's template.py appears below.

#--------------------------------
# Name:        template.py
# Purpose:
# Author:
# Created     23/06/2011
# Copyright:   (c) company name
# ArcGIS Version:   10
# Python Version:   2.6
#--------------------------------
import os
import sys
import arcpy
def do_analysis(*argv):
    """TODO: Add documentation about this function here"""
    try:
        #TODO: Add analysis here
        pass
    except arcpy.ExecuteError:
        print arcpy.GetMessages(2)
    except Exception as e:
        print e.args[0]
# End do_analysis function

# This test allows the script to be used from the operating
# system command prompt (stand-alone), in a Python IDE, 
# as a geoprocessing script tool, or as a module imported in
# another script
if __name__ == '__main__':
    # Arguments are optional
    argv = tuple(arcpy.GetParameterAsText(i)
        for i in range(arcpy.GetArgumentCount()))
    do_analysis(*argv)

An alternative template that you may wish to review can be found in GitHub at https://github.com/brian32768/ArcGIS_Python_Template.

  • 2
    Wow, how did it take this long for me to come across this?? – Paul Aug 18 '15 at 15:44
  • ESRI recently deleted most of their old blog entries. You can still find them in the Wayback Machine, go to Archive.org and search for the URL. For this one try web.archive.org/web/20151217180732/http://blogs.esri.com:80/… but all due respect I think my own template is better. :-) See github.com/brian32768/ArcGIS_Python_Template and send me suggestions. – brian h wilson Jun 4 '18 at 18:21
  • Thanks @brianhwilson - I'd not noticed that Esri blog overhaul, and I've now copied in their code, and added a link to yours. I've not looked at your code but from a quick read of your README.md it sounds like you are targeting Python Toolbox Tools in *.pyt files whereas theirs targets standalone Python scripts and Python Script Tools source/stored in Standard Toolboxes (*.tbx). – PolyGeo Jun 4 '18 at 22:31
4

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]
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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