Select to view content in your preferred language

Getting the user to set the workspace?

1968
1
07-20-2013 10:45 AM
DonalCasey
Emerging Contributor
Hi all,

I've got a question. I'm trying to write a python script. As I want to automate as much as possible, I'm just wondering would it be possible to make the workspace a variable so that all that the person has to do is input the workspace location and script will then find the necessary files?

Basically, I was wondering just by setting the workspace and the output file names would it be possible to then the script to work? For example, is it possible to tell Python to use the workspace as the default out location or 'out folder path' in the case of creating the file geodatabase?

The script is rather long but here is the first bit if anybody has any ideas:

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


I'm relatively new to Python so hopefully the above makes sense!

Thanks
Tags (2)
0 Kudos
1 Reply
by Anonymous User
Not applicable
If this is for a script tool, you can use [url=http://resources.arcgis.com/en/help/main/10.1/index.html#//018v00000047000000]GetParameterAsText[/ur...] to have a user-defined workspace like so:

# set the workspace
arcpy.env.workspace = arcpy.GetParameterAsText(0)


Or, if it will only be used as a stand alone script you can use raw_input.  The prompt for the user is contained inside the parenthesis and the interpreter will read the text the user types (or pastes) once the user has hit the enter key.

# set the workspace
arcpy.env.workspace = raw_input('Paste the path to the workspace and hit enter\n')
0 Kudos