Wednesday 19 August 2015

arcpy - Problem in reading mosaic_dataset path from a csv file


Based on this How to batch Check geometry in ArcGIS for a list of datasets from a CSV? code, i'm trying to extract the mosaic dataset path's within multiple folders. If i directly provide the mosaic_dataset path instead of fcs, the code works fine. But don't know why it's not reading the path from csv file. This is the path in the csv file(row 0) D:\QAQC\Test.gdb\Aus_Geology.gdb\250K_Geology. If i copy and replace fcs with exact same thing, the code works.


import csv
import arcpy
csvpath = r"D:\QAQC\Open2.csv"
with open(csvpath, "r") as csvfile:

fcs = [r[0] for r in csv.reader(csvfile)]
arcpy.ExportMosaicDatasetPaths_management(fcs,"D:\Test\Paths.dbf")

This is the error i am getting


File "", line 6, in File "c:\program files\arcgis\desktop10.3\arcpy\arcpy\management.py", line 11099, in ExportMosaicDatasetPaths raise e RuntimeError: Object: Error in executing tool



Answer



It would be helpful if you gave more details about the contents of your CSV file (is there more than one line?) and about what error you are seeing.


The main issue I see with your code is that you are making the variable fcs a list, and the arcpy tool you call is looking for a single input object. Passing a list might cause an error (I am not sure as you do not give details about the error message). If you CSV only contains one line, just modify the fcs variable like so:


fcs = str([r[0] for r in csv.reader(csvfile)][0])


If you have more than one line, you would need to put your arcpy tool in a loop to iterate over the items in the list.


Here's a complete code block that includes looping over the lines of your CSV:


import csv
import arcpy
from os import path
csvpath = r"D:\QAQC\Open2.csv"
csvdir = path.split(csvpath)[0]
with open(csvpath, "r") as csvfile:
fcs = [r[0] for r in csv.reader(csvfile)]
tables = []

for fc in fcs:
mosaicname = path.split(fc)[1] + '.dbf'
tablefile = path.join(csvdir, mosaicname)
tables.append(tablefile)
arcpy.ExportMosaicDatasetPaths_management(fc, tablefile)
arcpy.Merge_management(tables, r'D:\Test\Paths.dbf')
for eachTable in tables:
arcpy.Delete_management(eachTable)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...