I have multiple lines but only one is written to the csv file. What am I missing from an except of my code.
s = (fullpath + lyr_source)
with open('TEST.csv', 'w') as writeexcel:
a = csv.writer(writeexcel)
for line in :
a.writerow ([line])
I have the following and choose a random field name that is in one of the many feature classes I need returned.
I still have the error.
with open('TEST.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', lineterminator='\n')
fields = ['APN']
with arcpy.da.SearchCursor(s, fields) as s_cursor:
for row in s_cursor:
csvwriter.writerow(fields)
What is the error message?
edit: if you want to write the value of the current row, reference it in the row object.
csvwriter.writerow(row[0]) # would write the value in the first field in your field list
Traceback (most recent call last):
File "C:\Users\dunderwood\Desktop\LoopMXDListLayers.py", line 39, in <module>
with arcpy.da.SearchCursor(s, fields) as s_cursor:
RuntimeError: cannot open 'C:\...
Look closely at Blake's example for writing a path:
workingDir = r"C:\temp"
The r denotes a raw string, which means '\t' is not interpreted as a tab, as it otherwise would. Is this how you're writing your paths?
I have been looking at how to post code in geo net and building paths correctly. (Thanks Blake) I have only successfully formatted my original posting and still looking at building paths correctly.
I always use the raw as a path
r'C:\...'
Please post your entire script, exactly as it is, including the full error message.
# Import Modules
import arcpy,os,csv
#Set folder space
folder = r'C:\MY_TEMPLATES\CW'
#Loop All Mxd's in a folder and print out Info.
for filename in os.listdir(folder):
fullpath = os.path.join(folder, filename)
if os.path.isfile(fullpath):
basename, extension = os.path.splitext(fullpath)
if extension.lower() == ".mxd":
mxd = arcpy.mapping.MapDocument(fullpath)
dfs = arcpy.mapping.ListDataFrames(mxd)
for df in dfs:
layers = arcpy.mapping.ListLayers(mxd, "", df)
for layer in layers:
if layer.isFeatureLayer:
lyr_source = layer.dataSource
lyr_name = layer.name.encode("utf8", "replace")
s = (fullpath + lyr_source)
print s
with open('TEST.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', lineterminator='\n')
fields = ['*']
with arcpy.da.SearchCursor(s, fields) as s_cursor:
for row in s_cursor:
csvwriter.writerow(row)
#Close Excel File
csvfile.close()
print "Closed or Not: ",csvfile.closedRun Script: (I get the following which I want looped to write to csv, but the cursor currently retrieves fields)
C:\MY_TEMPLATES\CW\CW_ENV_FMSE_EDIT.mxdDatabase Connections\CW_ENV_uCW_ENV_vCW_ENV.sde\PW.EnvironmentalCompliance\PW.ServiceEstablishments
Traceback (most recent call last):
File "C:\Users\dunderwood\Desktop\LoopMXDListLayers.py", line 30, in <module>
with arcpy.da.SearchCursor(s, fields) as s_cursor:
RuntimeError: cannot open 'C:\MY_TEMPLATES\CW\CW_ENV_FMSE_EDIT.mxdDatabase Connections\CW_ENV_uCW_ENV_vCW_ENV.sde\PW.EnvironmentalCompliance\PW.ServiceEstablishments'
I'm confused by this line, which apparently concatenates two full file paths together:
s = (fullpath + lyr_source)Does this work differently with SDE paths than regular file paths?
I want both so I get the mxd name and feature class path
fullpath is C:\Users\dunderwood\Documents\MY_TEMPLATES\CityWorks\CW_ENV_FMSE_EDIT.mxd
lyr_source is Database Connections\CW_ENV_uCW_ENV_vCW_ENV.sde\PW.EnvironmentalCompliance\PW.ServiceEstablishments
I am not sure SDE vs regular file path.
The SearchCursor can only read a single file, so s should just be the lyr_source, if that's what you want to read.