1

我收到了 IOError: [Errno 2] No such file or directory 错误,我似乎无法弄清楚问题出在哪里。我检查了目录并且文件存在。另外我正在尝试通过许多文本文件运行这个程序,所以如果你能检查一下是否还有其他问题会很好!

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`

这是具体的错误:

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] 没有这样的文件或目录:u'20060705t2.csv'

4

1 回答 1

1

创建文件时,您需要预先添加每个文件的路径:

input = open(os.path.join(env.workspace, filename), 'r')
于 2013-09-10T15:37:02.477 回答