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
# 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)

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"""
        #TODO: Add analysis here
    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()))

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


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

# Check out extension and overwrite outputs
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]

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.