Python 如何修复IOError:[Errno 2]没有此类文件或目录错误?

Python 如何修复IOError:[Errno 2]没有此类文件或目录错误?,python,csv,ioerror,arcpy,Python,Csv,Ioerror,Arcpy,我收到一个IOError:[Errno 2]没有这样的文件或目录错误,我似乎无法找出问题所在。我已经检查了目录,文件已经存在。 此外,我正试图通过许多文本文件运行这个程序,所以如果你能检查一下,看看是否有任何其他问题,这将是伟大的 import glob import os import shutil import fileinput import arcpy from arcpy import env env.workspace = "h:/Python scripts/Tests2" li

我收到一个IOError:[Errno 2]没有这样的文件或目录错误,我似乎无法找出问题所在。我已经检查了目录,文件已经存在。 此外,我正试图通过许多文本文件运行这个程序,所以如果你能检查一下,看看是否有任何其他问题,这将是伟大的

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")
回溯(最近一次呼叫最后一次):
RunScript中的文件“C:\Python27\Lib\site packages\pythonwin\framework\scriptutils.py”,第325行
主目录中的exec codeObject__
文件“H:\Python scripts\Script1.py”,第45行,在
CsvTrack=打开(文件名2,“r”)

IOError:[Errno 2]没有这样的文件或目录:u'20060705t2.csv'

创建文件时,需要在每个文件的路径前加上前缀:

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

创建文件时,需要预先指定每个文件的路径:

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