1

We have a list of feature-classes which are in multiple gdb's in multiple folders. This code is opening the csv and doing "GetCount_management" for the listed feature_classes alone and printing the output. The code is printing correctly. Say, if there are 25 feature classes in the csv, it's listing all the counts of all the 25 files. But while saving it in csv, only the 25th count i.e the last file's count alone is getting saved. I don't know whats the issue.

import csv
import arcpy
from os import path
csvpath = r"D:\RTXQAQC\Open2.csv"
output = r'D:\Test\Test_2.csv'
csvdir = path.split(csvpath)[0]
with open(csvpath, "r") as csvfile:
    fcs = [r[0] for r in csv.reader(csvfile)]
for fc in fcs:
    result = arcpy.GetCount_management(fc)
    count = int(result.getOutput(0))
    print(count)
with open(output, 'wb') as f:
    csvwriter = csv.writer(f,delimiter=',')
    csvwriter.writerow([fc,result])
    f.close()
  • Please edit the question to define what you mean by "merge", and include your effort at this code within the question. – Vince May 3 '16 at 11:18
  • I've edited the question – joseph_k May 3 '16 at 11:58
  • 1
    If you use a with clause, you don't need to call close() on the file. – recurvata May 4 '16 at 14:33
1

I'm guessing this line:

with open(output, 'wb') as csvfile:

should be:

with open(output, 'wb') as csvwriter:
  • Getting this error AttributeError: 'file' object has no attribute 'writerow' – joseph_k May 3 '16 at 16:26
0

Try the following. I hope this will work.

import csv
import arcpy
import os
from os import path
csvpath = r"D:\RTXQAQC\Open2.csv"


output = r"D:\Test\testssss.csv"
csvfile=open(output, 'wb')                                          #binary opened
csvwriter=csv.writer(csvfile)

csvdir = path.split(csvpath)[0]
with open(csvpath, "r") as csvfile:
    fcs = [r[0] for r in csv.reader(csvfile)]
for fc in fcs:
    result = arcpy.GetCount_management(fc)
    count = int(result.getOutput(0))
    print(count)
    csvwriter.writerow(count)
  • Showing this error "Sequence expected". Actually i've edited the question. If possible, could you have a look – joseph_k May 4 '16 at 10:27
  • I just habe a look in your script. I thing you are writing the results outside for loops. you also did not write count , you wrote results – Shiuli Pervin May 4 '16 at 10:48
0

This is the code. As Shiuli Pervin pointed out in the comment, previously i was writing the results outside the for loop. I think, that's why it didn't get iterated.

import csv
import arcpy
from os import path
csvpath = r"D:\RTXQAQC\Open2.csv"
output = r'D:\Test\Test_2.csv'
csvdir = path.split(csvpath)[0]
csvwriter = csv.writer(f,delimiter=',')
with open(csvpath, "r") as csvfile:
     fcs = [r[0] for r in csv.reader(csvfile)]
     with open(output, 'wb') as f:
         for fc in fcs:
             result = arcpy.GetCount_management(fc)
             count = int(result.getOutput(0))
             print(count)
             csvwriter = csv.writer(f,delimiter=',')
             csvwriter.writerow([fc,result])

     f.close() 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.