gpt4 book ai didi

python - 如何修复 IOError : [Errno 2] No such file or directory error?

转载 作者:太空宇宙 更新时间:2023-11-03 18:49:33 25 4
gpt4 key购买 nike

我收到了 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'

最佳答案

创建文件时需要在每个文件前面添加路径:

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

关于python - 如何修复 IOError : [Errno 2] No such file or directory error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18707276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com