2

I just can't figure out how to feed a list of files into an arcpy tool...

I have

 List_tif.append(os.getcwd()+"\\Stage1\\"+name+"\\GeoTif\\"+name+"_"+str(ddp.pageRow.PageName) + ".tif;")

That generates the list and then I try to feed it into arcpy

arcpy.MosaicToNewRaster_management(List_tif,os.getcwd()+"\\Stage2\\jp2", name+"_tif.jp2", "90019.prj", "", "", "3", "LAST","FIRST")

but I get an error as the tools is expecting file1.tif; file2.tif etc but is getting a List

'P:2012円183円_TownPlanning_Symbology\Working\Raster_Layer_Creation\Stage1\LS\GeoTif\LS_O10.png;', 'P:2012円183円_TownPlanning_Symbology\Working\Raster_Layer_Creation\Stage1\LS\GeoTif\LS_O11.png;', 'P:2012円183円_TownPlanning_Symbology\Working\Raster_Layer_Creation\Stage1\LS\GeoTif\LS_O12.png;', 'P:2012円183円_TownPlanning_Symbology\Working\Raster_Layer_Creation\Stage1\LS\GeoTif\LS_O13.png;']

I tried

PNGList=string.split(List_png, ";")

but this doesn't work either. This should be simple (:-

EDIT 1:

Thanks guys...I tried the list rasters as well but it doesn't even generate the list

>>> rasters=arcpy.ListRasters('*')
>>> rasters
>>> files=arcpy.ListFiles()
>>> files
>>> import glob
>>> rasters=glob.glob('*.png')
>>> rasters
['BA25k_A2.png', 'BA25k_A3.png', 'BA25k_A4.png', 'BA25k_A5.png', 'BA25k_A6.png', 'BA25k_A7.png', 'BA25k_A8.png', 'BA25k_B10.png', 'BA25k_B11.png', 'BA25k_B2.png', 'BA25k_B3.png', 'BA25k_B4.png', 'BA25k_B5.png', 'BA25k_B6.png', 'BA25k_B7.png', 'BA25k_B8.png', 'BA25k_B9.png', 'BA25k_C10.png', 'BA25k_C11.png', 'BA25k_C12.png', 'BA25k_C2.png', 'BA25k_C3.png', 'BA25k_C4.png', 'BA25k_C5.png', 'BA25k_C6.png', 'BA25k_C7.png', 'BA25k_C8.png', 'BA25k_C9.png', 'BA25k_D10.png', 'BA25k_D11.png', 'BA25k_D12.png', 'BA25k_D13.png', 'BA25k_D2.png', 'BA25k_D3.png', 'BA25k_D4.png', 'BA25k_D5.png', 'BA25k_D6.png']
>>> 

I didn't mean to ask this specifically for the arcpy mosaic tool as passing the list of files is important for lots of other tools as well such as creating a mosaic dataset, filling a gdb etc.

I also tried your vtab example

vtab = arcpy.ValueTable(1)
for tif in List_tif:
 vtab.addRow(tif)
print "Generating jp2 from png tiles @"+str(datetime.datetime.now())
##PNGList=string.split(List_png, ";")
arcpy.MosaicToNewRaster_management(vtab,os.getcwd()+"\\Stage2\\jp2", name+"_tif.jp2", "90019.prj", "", "", "3", "LAST","FIRST")

but I still get the bands error.

asked Oct 27, 2012 at 2:32
3
  • 1
    possible duplicate of Cannot get arcpy.MosiacToNewRaster to work with list Commented Oct 27, 2012 at 2:38
  • If its not a duplicate perhaps update Question to say why answer(s) there don't provide a solution here Commented Oct 27, 2012 at 2:50
  • Please see my updated question...it was supposed to be more general on passing files into arcpy tools Commented Oct 27, 2012 at 3:17

1 Answer 1

4

Are you remembering to set your arcpy.env.workspace before using the various arcpy list functions (ListRaster, ListFiles, etc.)?

Also many arcpy functions do take Python list objects for some arguments. They are denoted in the help by syntax such as [input_raster,...]. Indeed, MosaicToNewRaster's input_rasters argument does.

There may be other functions that require a semicolon-delimited string instead. You can easily create one using ';'.join(myList), for example.

You could also greatly simplify and make more robust your path building code by taking advantage of os.path.join() and string formatting (either the newer .format() method or the older modulo (%) method) instead of using the concatentation operator (+) and hardcoding \\'s everywhere.

Your second line doesn't work because in your first line you are conflating the two methods (passing a list and passing a semicolon-delimited list). Thus you are a building a list of strings, each string having a semicolon at the end, which is not a valid pathname. It would probably work if you just removed the ; at the end of your first line.

Your third line doesn't work because you want to join, not split the list. It might seem counterintuitive, but I recommend reading Dive Into Python's Joining Lists and Splitting Strings section for a better explanation.

answered Oct 27, 2012 at 4:57
1
  • I had mistakenly done "arcpy.env=os.getcwd()" rather than "arcpy.env.workspace=os.getcwd()" and so the arcpy.List* didn't work. Once this was corrected merge worked fine. Thanks for your time... Commented Oct 27, 2012 at 7:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.