2

I am very new to Python and am trying to complete my final project with not a lot of success.

My code works without functions, the problem is I need it to work with functions. In the main(): function I want the user to enter an .shp file path. Then I have an isValid(inPath): function that I want to call the file path the user entered and validate if the file exists. I have done this in many assignments before with .csv and .gpx files, but I am not having success with .shp files. When I keep everything under the main function, it works no problem.

Here is my code currently:

**# Import Modules:**
import arcpy
from arcpy import env
from arcpy import mapping
import os.path
**# Set workspace**
env.workspace = #this is where I set my workspace
env.overwriteOutput = True
**# Checks if shapefile is valid**
def isValid(inPath):
 if arcpy.Exists(inPath):
 print 'Exists'
 else:
 print ' Does NOT exist'
def main():
 shpFile = raw_input("Enter a Map Document path: ") # User inputs a shapefile
 shpFile = shpFile.replace("\\","/") #replaces backslashes with forward slashes
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Dec 8, 2013 at 23:46

1 Answer 1

6

It's not working because your not calling it :)

Try something like this

def isValid(inPath):
 if arcpy.Exists(inPath):
 print 'Exists'
 else:
 print ' Does NOT exist'
def main():
 shpFile = raw_input("Enter a Map Document path: ") # User inputs a shapefile
 shpFile = shpFile.replace("\\","/") #You shouldn't need to do this
 isValid(shpFile)

but really isValid should return something not just print values:

def isValid(inPath):
 return arcpy.Exists(inPath)
def main():
 shpFile = raw_input("Enter a Map Document path: ") # User inputs a shapefile
 shpFile = shpFile.replace("\\","/") #You shouldn't need to do this
 if isValid(shpFile):
 print "YAY"
answered Dec 9, 2013 at 0:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.