Nesting Try and Except

This is the place for queries that don't fit in any of the other categories.

Nesting Try and Except

Postby binkey24 » Tue Apr 01, 2014 1:19 pm

Hey all, I am new to python and have come quite stuck. I have searched high and low for an answer with no prevail. I am trying to copy documents in one folder to another through a nested try and except block. The reason it is nested is I am looking at the script conducting three processes in sequential order, copy - rename - reproject. For some reason whenever I run the script the output can not complete copy occurs and I am unsure why. I copied the script outside of the nesting and found the same issue was occurring. Any advice would be greatly appreciated.

Code: Select all
#Import ARCPY
import arcpy

# Create an error class
class NoExist(Exception):
    pass
#   -------------------------------------------------------------------
# The following code is using nested try and except blocks
#   -------------------------------------------------------------------

try:

#   -------------------------------------------------------------------   
#   The following code deals with the copying of the original files
#   This ensuring that the original documentation is still accessible
#   as provided
#   -------------------------------------------------------------------
    try:

        # Create an error class
        class NoExist(Exception):
            pass
       
#       Acquire the target folder
        targetFolder = arcpy.GetParameterAsText(0)
       
#       Get the path to the copy folder
        copyFolder = arcpy.GetParameterAsText(1)
       
#       If target folder or copy folder do not exist raise the error class

        if not acrpy.Exists(targetFolder):
            raise NoExist
        elif not arcpy.Exists(copyFolder):
            raise NoExist

#       Set the workspace within the target folder, using the overwrite
#       will assist in removing the potential issue of the script not
#       working due to a pre existing folder

        arcpy.env.workspace = targetFolder
        arcpy.env.overwriteOutput = True

#       Provide list of all files within the current workspace

        fileList = arcpy.ListFiles()

        # Loop all files within the list to copy to new location
        for fileName in fileList:
            arcpy.Copy_management (fileName, copyFolder +'/'+ fileName)

        # Report Success Message
        arcpy.AddMessage("Files copied succesfully.")

    except NoExist:
        arcpy.AddMessage("The path supplied is incorrect! Please try again.")

    except:
#       Report Error Message to user
        arcpy.AddError("Copy could not be completed.")
#       Provides information about tools that may have prevented completion                 
        arcpy.AddMessage(arcpy.GetMessages())

# Code below is just the test to say skipping nested
except NoExist:
    arcpy.AddMessage("Skipping nested.")
except:
#   Report Error Message to user
    arcpy.AddError("skipping nested.")
#   Provides information about tools that may have prevented completion                 
    arcpy.AddMessage(arcpy.GetMessages())
binkey24
 
Posts: 1
Joined: Tue Apr 01, 2014 1:13 pm

Re: Nesting Try and Except

Postby micseydel » Tue Apr 08, 2014 9:51 pm

The code and question here are really hard to get a grip on. It sounds like you're saying you've tried simpler code that reproduces your problem; as far as helping you, seeing the least amount of code that reproduces the problem is ideal. You should also indent comments to the same level as the code the comment is referencing, rather than bounce back and forth between proper indentation and 0 indentation.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1361
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Yoriz and 3 guests