I'm using python 2.7 on windows platform. (I don't have a choice to upgrade - cannot import any non standard modules...work constraints.) I have written a csv file from a feature class. I then am attempting to re-format text in a column. For some reason I am unable to open the csv.
I get the following errors:
D:\JJ_Development\Projects\Basemap\Transportation>new.py
D:\JJ_Development\Projects\Basemap\Transportation
Traceback (most recent call last):
File "D:\JJ_Development\Projects\Basemap\Transportation\new.py", line 199, in <module>
newlabel()
File "D:\JJ_Development\Projects\Basemap\Transportation\new.py", line 97, in newlabel
with open(rc_existing, 'rb') as csvfile:
IOError: [Errno 2] No such file or directory: 'D:\\JJ_Development\\Projects\\Basemap\\Transportation\\roadcore'
- Both the csv and the python script are in my current working directory.
- The csv is not read only.
- I can open the csv in both notepad++ and arcmap with no issues.
- The file is not locked...I have restarted computer and have changed the name as well as copying over a fresh script.
I'm posting a snippet of csv creations (works) and then errors on the line "with open" (obviously this is not the full code)
#********DATA AND PATHS********
curr_dir = os.getcwd()
print(curr_dir)
arcpy.env.workspace = (curr_dir)
#mxd = arcpy.mapping.MapDocument(r'D:\JJ_Development\Projects\Basemap\Transportation\Labels.mxd')
#in_fc = arcpy.mapping.ListLayers(mxd, 'RoadCore_test2')[0]
fc = (r'D:\JJ_Development\Projects\Basemap\Transportation\Roads.gdb\RoadCore_Existing')
fields = ["ID", "ADMIN_ORG", "RTE_CN", "GIS_MILES"]
identical = ["ID", "ADMIN_ORG", "RTE_CN"]
labels = []
rd_names = (' ROAD', ' STREET', ' DRIVE', ' BLUFF', ' COVE',' LOOP', ' LANE', ' RD', ' LN', ' DR')
#********Initial FS Roadcore Existing Data Cleanup********
rc_existing = os.path.join(curr_dir, 'roadcore')
def test():
fields = ["ID", "ADMIN_ORG", "RTE_CN", "GIS_MILES"]
with open('{}.csv'.format(rc_existing), 'wb') as outf:
dw = csv.DictWriter(outf, fieldnames=fields)
dw.writeheader()
with arcpy.da.SearchCursor(fc, fields) as rows:
for row in rows:
if row[3] == 0:
pass
elif row[3] == None:
pass
else:
dw.writerow(dict(zip(fields, row)))
print "File written: {}".format(rc_existing)
outf.close()
#********FORMATTING LABELS********
def newlabel():
with open(rc_existing, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
print('csv open')
# with arcpy.da.SearchCursor(rc_existing, fields) as sc:
# for row in sc:
# if (row[0].startswith(' ')):
# labels.append('{}'.format(row[0].replace(' ', '')))
if __name__ == '__main__':
test()
newlabel()
rb
, which is read-only/binary. Then later in the code, at the linewith open('{}.csv
, you're opening it in modewb
, which is write-only/binary. There's a list of codes available to you here. Are you sure you're opening it in the correct mode? What if you tried, simply, moder
? (If you're going to write in the file, tryr+
.)