Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

Using Loop function with arcpy.Exists()?

$
0
0

I have a code snippet using elif that checks if a file exists and continues to check through the letters of the alphabet (for example _A, _B, _C etc). All works fine but I think there must a better (or cleaner) way to write the code using a loop function instead of the way I have done.

#Create MXD file path
mxdOutput = wrFolder + "/M_" + WorkrequestRaw +"_01.mxd"
mxdPath = wrFolder + "/M_" + WorkrequestRaw +"_01"

#check for latest version and create next letter
if  not arcpy.Exists(mxdOutput):
    mxdOutput = wrFolder + "/M_" + WorkrequestRaw +"_01.mxd"
    pdfversion = ""
elif not arcpy.Exists(mxdPath +"_A.mxd"):
    mxdOutput = wrFolder + "/M_" + WorkrequestRaw +"_01_A.mxd"
    pdfversion = "A"
elif not arcpy.Exists(mxdPath +"_B.mxd"):
    mxdOutput = wrFolder + "/M_" + WorkrequestRaw +"_01_B.mxd"
    pdfversion = "B"
elif not arcpy.Exists(mxdPath +"_C.mxd"):
    mxdOutput = wrFolder + "/M_" + WorkrequestRaw +"_01_C.mxd"
    pdfversion = "C"
elif not arcpy.Exists(mxdPath +"_D.mxd"):
    mxdOutput = wrFolder + "/M_" + WorkrequestRaw +"_01_D.mxd"
    pdfversion = "D"

I am a self taught beginner and trying to keep the code tidy. Welcome any comments on a better methodology?

Update poorly written question: What I am trying to do is if file name exists write new file called ‘filenameA’ and if ‘filenameA’ exists write ‘filenameB’ and if ‘filenameB exists write ‘filenameC’ etc.


Viewing all articles
Browse latest Browse all 767

Trending Articles