How to input a shapefile in my Python script?

4790
4
11-13-2013 02:43 AM
ChloeAsmar
New Contributor III
Hello all!

So I am really new to Python.

I had to convert a Matlab Script into Python script in order to be able to add it as a script tool in Arcmap.

I am having a hard time figuring out how to input a shapefile into my script. My whole script runs based on the shapefile so I have no way of checking if it works unless it reads the shapefile.

Thanks in advance!!
Tags (2)
0 Kudos
4 Replies
XanderBakker
Esri Esteemed Contributor
Hello all!

So I am really new to Python.

I had to convert a Matlab Script into Python script in order to be able to add it as a script tool in Arcmap.

I am having a hard time figuring out how to input a shapefile into my script. My whole script runs based on the shapefile so I have no way of checking if it works unless it reads the shapefile.

Thanks in advance!!


If you run the script standalone, then you just use the path to the ".shp" file like this:

import arcpy
inputShp = r'C:\Path\to\some\shapefile.shp'
outputShp = r'C:\Path\to\other\shapefile.shp'

# do something with the shapefile
arcpy.CopyFeatures_management(inputShp, outputShp)



If you added the script  to a Toolbox and the shapefile is a parameter, define the parameter as shapefile and read it from the script like:

import arcpy
inputShp = arcpy.GetParameterAsText(0)

# do something with the shapefile



If this is not what you're looking for then share the code and we can have a look at it.

Kind regards,

Xander
0 Kudos
DouglasSands
Occasional Contributor II
Have you checked out this article about working with inputs? If you need to actually do something with an ArcGIS tool, you may also need to use Make Feature Layer first, depending on the tool.
0 Kudos
ChloeAsmar
New Contributor III
Thanks a lot for your answers 😄 It worked!
0 Kudos
ChloeAsmar
New Contributor III
Thanks a lot for your answers 😄 It worked!
0 Kudos