# Writing ArcPy cursor results to text file?

I don't know how to write the result in a text file, can anyone help me?

import arcpy
rows = arcpy.SearchCursor("E:/Arcgis/lx.gdb/WuChangJieDaokou_onlyweight", "", "", "", "VEHICLEID A; TIME A")
currentState = ""
f=open('C:/Users/GWJ/Desktop/111.txt','w')
for row in rows:
currentState = row.VEHICLEID
f.write(row.VEHICLEID,row.NEAR_X,row.NEAR_Y,row.TIME)


Runtime error Traceback (most recent call last): File "", line 7, in TypeError: function takes exactly 1 argument (4 given)

Runtime error Traceback (most recent call last): File "", line 7, in TypeError: expected a character buffer object

• Rule #1 of ArcGIS Cursors: Do not use arcpy.SearchCursor. Instead use DataAccess cursors (arcpy.da.SearchCursor). Old-style cursors are deprecated, slow, and anti-Pythonic. Learn to use DA cursors now. Rows will become lists and life will be wonderful. Apr 5, 2018 at 21:46

Are you trying to write multiple lines to the file or multiple values in the same line? Take into account that the f.write() method takes only one argument which is the value to be written in a specific line. Using the f.writelines() method allows you to specify a list of values to be written in a specific number of lines (depends on the size of the listt you pass to the method)

You could try using the following line if you want the values to be in the same line,

f.write("{0}, {1}, {2}, {3}".format(row.VEHICLEID,row.NEAR_X,row.NEAR_Y,row.TIME))

or the following line if you want them to be in separte lines,

f.writelines([row.VEHICLEID,row.NEAR_X,row.NEAR_Y,row.TIME])

Looks like you may have forgotten to use "open" instead of "file" in your opening of the file:

*f=file('C:/Users/GWJ/Desktop/111.txt','w')


should be:

f=open('C:/Users/GWJ/Desktop/111.txt','w')


but best practice is to use a "with" so you don't need to worry about closing your file when done/error/hang/etc:

with open('C:/Users/GWJ/Desktop/111.txt','w') as f:


• But it reported the same error after modification
– Girl
Apr 5, 2018 at 14:34
• What exactly are line 9 and 10 in your code? Apr 5, 2018 at 14:37
• As an aside, I would use a "with" statement with your search cursor as well: with arcpy.SearchCursor(whatever) as rows: Apr 5, 2018 at 14:38
• +1 for using the with statement for best practice Apr 5, 2018 at 14:46
• I have already updated the newest error in my question.If I change the parameter to one,It will also report an error as above.
– Girl
Apr 5, 2018 at 14:48

I would urge you to avoid using the deprecated, non-data access module ArcGIS cursor. The following approach generates a list of rows as tuples using a list comprehension then writes the lines to a text file using writelines:

import arcpy

fc = r'C:\path\to\your\geodatabase.gdb\featureclass'
txt = r'C:\path\to\your\txt_file.txt'

lines = [row for row in arcpy.da.SearchCursor(fc, ("VEHICLEID", "NEAR_X", "NEAR_Y", "TIME"))]

with open(txt, 'w') as txtfile:
txtfile.writelines(str(i).strip("()") + "\n" for i in lines)