Skipping over an already existing file during the for loop

4468
6
05-28-2013 11:14 AM
D__SamuelRajasekhar
New Contributor III
Hello All,

I am running a very simple script where I made a list of geo-referenced aerial photos as a text (CSV) file and then using this list in a for statement to re-project the aerial photos.

I am having a problem where there seems to be files with the same name and the script stops when it encounters an already existing file (the aerial photos is re-projected and while attempting to re-project the duplicate) the script stops.

error message:
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000725: Output Raster Dataset: Dataset X:\Archive\1930_1949\MO\JP2_Zone15\1939_03_11_11_310.jp2 already exists.
Failed to execute (ProjectRaster).
I tried to use the pass it simply passes over the existing files but does not run the loop further.

Any & all help is greatly appreciated.
Tags (2)
0 Kudos
6 Replies
MathewCoyle
Frequent Contributor
A simple "if not" statement would work, try posting the code you are running for more detailed help.
0 Kudos
D__SamuelRajasekhar
New Contributor III
A simple "if not" statement would work, try posting the code you are running for more detailed help.


import arcpy
import os
from arcpy import env

# Location where the images will be copied to:

arcpy.env.workspace = r'X:\Archive\1930_1949\MO\JP2_Zone15'


with open(r'X:\Archive\Python_Scripts\UTM_15.txt', 'r') as f:
        prj = f.readlines()[0]


outPath = arcpy.env.workspace

listFiles = open(r'X:\Archive\1930_1949\MO\Index\List_Files_Zone15.csv', 'r')
for inputRasters in listFiles.readlines():
        inputFilenames = os.path.splitext(os.path.basename(inputRasters))[0]
        print inputFilenames
        print inputRasters

        outPut = os.path.join(outPath,inputFilenames + ".jp2")

        print outPut
        print ''
        arcpy.ProjectRaster_management(inputRasters, outPut, prj)


I am trying to add this somewhere

if (os.path.exists('outPut')):
                os.remove('outPut')
0 Kudos
MathewCoyle
Frequent Contributor
Are you trying to overwrite the existing output file or skip over that iteration of your loop if it already exists?
0 Kudos
RhettZufelt
MVP Frequent Contributor
import arcpy
import os
from arcpy import env

# Location where the images will be copied to:

arcpy.env.workspace = r'X:\Archive\1930_1949\MO\JP2_Zone15'


with open(r'X:\Archive\Python_Scripts\UTM_15.txt', 'r') as f:
        prj = f.readlines()[0]


outPath = arcpy.env.workspace

listFiles = open(r'X:\Archive\1930_1949\MO\Index\List_Files_Zone15.csv', 'r')

Fname_old = ""
cnt = 0

for inputRasters in listFiles.readlines():
        inputFilenames = os.path.splitext(os.path.basename(inputRasters))[0]
        if Fname_old != inputFilenames:
            cnt = 0
        print inputFilenames
        print inputRasters

        outPut = os.path.join(outPath,inputFilenames + ".jp2")
        print outPut
        print ''
 

       if arcpy.Exists(outPut):                                                          ###  This will delete the file if it already exists
            arcpy.Delete_management(outPut)                                      ###  However, if it is in your list, I suspect that you want it regardless of filename already exists

       if arcpy.Exists(outPut):                                                          
            cnt += 1
            Fname_old = inputFilenames
            outPut =  os.path.join(outPath,inputFilenames + "_" + str(cnt) + ".jp2")   ###  This would append an _# (1,2,3, etc) to the of the filename and project/save it anyway


       arcpy.ProjectRaster_management(inputRasters, outPut, prj)






Might try this to remove the raster if exists.  Or the second option that will just append an _# to the end of filename if already exists.

Of course, this would only work if the "matching" filenames were consecutive in your list (otherwise, the cnt gets set back to zero).

Otherwise, might look into loading your output filenames into a list, then when iterating you can check to see if the file already exists, if so, append a number or break the loop.


MyList = []
outPath = r'X:\Archive\1930_1949\MO\JP2_Zone15\'

for inputRasters in listFiles.readlines():
        inputFilenames = os.path.splitext(os.path.basename(inputRasters))[0]
        if inputFilenames not in MyList:
            MyList.append(inputFilenames)
        else:
            continue
            

 for filename in MyList:
      outPut = os.path.join(outPath,filename + ".jp2")
      arcpy.ProjectRaster_management(filename, outPut, prj)


Of course, if you really don't want it to do anything when the filenames are duplicates, you can read your filelist into a python list, then make a set of it.
Sets will remove any duplicates and the resultant set will have unique values only.


for inputRasters in listFiles.readlines():
        inputFilenames = os.path.splitext(os.path.basename(inputRasters))[0]
        MyList.append(inputFilenames)
MySet = set(MyList)
for rasts in MySet:
    outPut = os.path.join(outPath,rasts + ".jp2")
    arcpy.ProjectRaster_management(rasts, outPut, prj)







R_
0 Kudos
D__SamuelRajasekhar
New Contributor III
Sorry for the late response.

Thanks for all the fantastic responses.

I am trying to skip over the existing file in the iteration loop.

I will try to incorporate the code/s supplied (and learn more too in the process).

In the mean time (yesterday) I added
arcpy.env.overwriteOutput = True

That overwrote the existing files (if any) and I was able to achieve the result.

Now with more sophisticated options I will incorporate (with learning) the elements of the code supplied.

Thanks to all for your efforts.
0 Kudos
MichaelDulin
New Contributor II

this worked for me, see the if/else statements

# Import arcpy module

import arcpy

from arcpy import env

from arcpy.sa import*

import os

import sys

# Local variables:

env.workspace = "R:\\moriver\\Aerials\\Aerials1930\\Images\\"

outs = "E:\\1930_IMG\\"

rasterList =[]

count = 0

for dirname, dirnames, filenames in os.walk ('R:\\moriver\\Aerials\\Aerials1930\\Images\\'):

    for subdirname in dirnames:       

         print os.path.join(dirname, subdirname)

         env.workspace = os.path.join(dirname, subdirname)

         rasters = arcpy.ListRasters()

         for inRaster in rasters:

            Name = inRaster[:-4]

            print Name

            Name = inRaster

            outs = "E:\1930_IMG" + Name

            if arcpy.Exists (outs):

                print "it already exists"

            else:

                outras = "E:\\1930_IMG\\"

                arcpy.RasterToOtherFormat_conversion (inRaster, outras, "IMAGINE Image")

                print "Success"

0 Kudos