Search code examples
pythonbatch-fileasciiarcgisraster

Raster to ASCII - adding processing multiple files piece of code in Python


I wrote a piece of code in python that converts a raster file to ascii. Now, I need to make it handle possibly all files in the folder. Also, at the end save ascii files with the same name that the original with a suffix added. I am a total newbee in python and i promise i did my homework, i could just not make the batch processing work on my own. Any help will be so much appreciated!!

import arcpy
from arcpy import env
env.workspace = "C:/Data"
inRaster = ("test.img")
outASCII = "c:/output/test3.asc"
arcpy.RasterToASCII_conversion(inRaster, outASCII)

Solution

  • Try this out:

    import os
    dir_name = ...
    for filename in os.listdir(dir_name):
        if not filename.endswith(".img"): continue
        full_path = os.path.join(dir_name, filename)
        outASCII = '%s.asc' % (full_path,)
        arcpy.RasterToASCII_conversion(full_path, outASCII)
    

    It gets all the filenames ending in .img in the directory dir_name and passes it to your conversion function.