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"
arcpy.env.overwriteOutput = True
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)
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)
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)
if (os.path.exists('outPut')): os.remove('outPut')
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.