Nakster09
Nakster09

Reputation: 11

How do i fix IOError: [Errno 2] No such file or directory error?

I have received an IOError: [Errno 2] No such file or directory error and I cant seem to figure out where the problem is. I have checked the directory and the file exists. Also I am trying to run this program through many text files, so if you could check to see if there are any other problems that would be great!

import glob
import os
import shutil
import fileinput
import arcpy
from arcpy import env
env.workspace = "h:/Python scripts/Tests2"

list_of_files = glob.glob("h:/Python scripts/Tests2/*.txt")
root_path = 'h:/Python scripts/Tests2/'

for file_name in list_of_files:
    input = open(file_name, 'r')   
    output = open(file_name.replace('.txt', 't2.csv'), "w")  
    for line in input:       
        str = line.strip("    dd/mm/yyyy hh:mm:ss         kA\t")
        str = str.replace("date", "ddmmyyyy,hhmmss")
        str = str.replace(" lat.    long.   amp.", " lat,long,ka")
        str = str.replace("id   ", "id,")
        str = str.replace(" ", ",")
        str = str.replace(" ", ",")
    output.write(str)
    input.close()
    output.close()


root_path2 = 'h:/Python scripts/Tests2/'
infile = arcpy.ListFiles("*t2.csv")
coordinatesys = 'H:\Python scripts\modeltests\NAD 1983.prj'
#infile = glob.glob("h:/Python scripts/Tests2/*scv.txt")
for file_name2 in infile:
    print infile
    print file_name2

    out_name = file_name2.replace('t2.csv', 'p2.shp')
    arcpy.CreateFeatureclass_management(root_path2, out_name, "Point", "", "DISABLED", "DISABLED", coordinatesys)
    arcpy.AddField_management(out_name, "ddmmyyyy", "TEXT")
    arcpy.AddField_management(out_name, "hhmmss", "TEXT")
    arcpy.AddField_management(out_name, "lat", "LONG")
    arcpy.AddField_management(out_name, "long", "LONG")
    arcpy.AddField_management(out_name, "ka", "LONG")

    print out_name    

    CsvTrack = open(file_name2, "r")

    headerLine = CsvTrack.readline()
    valueList = headerLine.strip().split(",")
    print valueList

    daysValueIndex = valueList.index("ddmmyyyy")
    timeValueIndex = valueList.index("hhmmss")
    latValueIndex = valueList.index("lat")
    lonValueIndex = valueList.index("long")
    kaValueIndex = valueList.index("ka")

    cursor = arcpy.InsertCursor(out_name)
    for Rows in CsvTrack.readlines():
        segmentedPoint = Rows.split(",")
        daysValue = segmentedPoint[daysValueIndex]
        timeValue = segmentedPoint[timeValueIndex]
        latValue = segmentedPoint[latValueIndex]
        lonValue = segmentedPoint[lonValueIndex]
        kaValue = segmentedPoint[kaValueIndex]
        vertex = arcpy.CreateObject("Point")
        vertex.X = lonValue
        vertex.Y = latValue
        feature = cursor.newRow()      
        feature.days = daysValue
        feature.time = timeValue
        feature.shape = vertex
        feature.ka = kaValue
        cursor.insertRow(feature)
    del cursor`

Here is the error specifically:

Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line   325, in RunScript
exec codeObject in __main__.__dict__
File "H:\Python scripts\Script1.py", line 45, in <module>
CsvTrack = open(file_name2, "r")

IOError: [Errno 2] No such file or directory: u'20060705t2.csv'

Upvotes: 1

Views: 6360

Answers (1)

garnertb
garnertb

Reputation: 9584

You need to prepend the path to each file when you create the file:

input = open(os.path.join(env.workspace, filename), 'r')

Upvotes: 1

Related Questions