2

I am creating a module in arcpy that will loop through a folder with csv files in it. I have both of them written, and they work fine on my computer. However when I send them to anyone else they won't run. I do not have any hard coded paths in them and cannot figure out the problem. The error says 'IOError: [Errno 2] no such file or directory:u'COTS_234AR.csv'' in regards to the line

#Open File
vehicle = open(input, "r")

Am I overlooking something?

Code that calls module:

#Import Modules
import arcpy
from arcpy import env
import sys 
import csv
import Script1

#environment settings
arcpy.env.overwriteOutput = True

#set workspace parameter: ONLY USER DEFINED INPUT-
#SHOULD BE IN FORMAT SIMILAR TO "M:\GIS\FinalExam"
filename = arcpy.env.workspace = sys.argv[1]

#create list of CSV files in folder
csvs = arcpy.ListFiles("*.csv")

#Create for loop of CSV files
try:
   for csv in csvs:
     #set parameters
        input = csv
        desc = arcpy.Describe(csv)
        output = (desc.basename + ".shp")
        #call in module
        Script1.scriptOne(input, filename, output)

except:
   print "There has been an error processing the scriptOne module."#
#retrieve all messages 
print(arcpy.GetMessages())

This is the actual module:

def scriptOne(input, filename, output):
    #import modules
    import csv
    import arcpy
    from arcpy import env

    #environment settings
    env.overwriteOutput = True

    #Open File
    vehicle = open(input, "r")

    #create csvReader object to process the header
    csvReader = csv.reader(vehicle)
    header = csvReader.next()
    latIndex = header.index("LAT")
    lonIndex = header.index("LON")
    out = arcpy.CreateFeatureclass_management(filename +"\\", output, "POLYLINE", "", "", "", 4326) 
    spatialRef =arcpy.Describe(out).spatialReference

    # Write the array to the feature class as a polyline feature
    vertexArray = arcpy.Array()

    # Loop through the lines in the file and get each coordinate
    for row in csvReader:
        # Get the lat/lon values of the current GPS reading
        lat = row[latIndex]
        lon = row[lonIndex]
        # Make a point from the coordinate and add it to the array
        vertex = arcpy.Point(lon,lat)
        vertexArray.add(vertex)

        #create insert cursor to make polyline shapefile
    with arcpy.da.InsertCursor(output, ("SHAPE@",)) as cursor:
        polyline = arcpy.Polyline(vertexArray, spatialRef)
        cursor.insertRow((polyline,))  
6
  • 1
    What do you mean by "they won't run"? Do they give errors? Do they do nothing? Do they look like they are running but produce no output?
    – Midavalo
    Commented May 4, 2016 at 18:09
  • They receive an error citing the ' vehicle = open(input, "r") ' line of code.
    – user65111
    Commented May 4, 2016 at 18:10
  • 2
    please edit your question to include the entire error message
    – Midavalo
    Commented May 4, 2016 at 18:11
  • Is that your added text? PATH OF CSV FILE HERE
    – Midavalo
    Commented May 4, 2016 at 18:19
  • yes, the csvs file name appears there. such as u'COTS_234AR.csv'
    – user65111
    Commented May 4, 2016 at 18:21

1 Answer 1

1

As csv isn't an arcpy module, it can't see arcpy.env.workspace to know your workspace path. Because of that it's looking for COTS_234AR.csv and not M:\GIS\FinalExam\COTS_234AR.csv (or whatever your workspace path is).

You need to pass input as a full path to the csv, not just the csv name.

import os # Add os to your imports

for csv in csvs:
    #set parameters
    input = os.path.join(filename, csv) # Full path to CSV file
    desc = arcpy.Describe(csv)
    output = (desc.basename + ".shp")
    #call in module
    Script1.scriptOne(input, filename, output)

Alternatively you could use arcpy.TableToTable_conversion to import your csv file as a temp table that an arcpy.da.SearchCursor() can access and then process from that to create your lines.

Your Answer

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

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