AnsweredAssumed Answered

Using Arcpy Try Except statement

Question asked by NEOMAPPER on Feb 21, 2018
Latest reply on Feb 21, 2018 by NEOMAPPER

I am trying to use the Try Except statement and I was wondering if I set it up correctly because I get an error when I run the script. I'm not sure if the error is due to the try Except or bad scripting on my part.

 

The script checks for a connection to NEO_1. If the connection exists then it checks all the versions name. This is where I seem to get an error. Since the user already exists the break should stop the script before prompting an error?

 

 

 

Empty Workspace Cache
Done
Connections Exists: NEO_1
Searching for User Credentials..
Windows Credentials: EGAS\jnmiller
Checking SDE for existing Credentials..
Executing: CreateVersion "Database Connections\NEO_1.sde" sde.DEFAULT jnmiller PROTECTED
Start Time: Wed Feb 21 11:38:41 2018
ERROR 001148: Cannot create a version with the specified name.
Version already exists [jnmiller][STATE_ID = 99689]
Failed to execute (CreateVersion).
Failed at Wed Feb 21 11:38:42 2018 (Elapsed Time: 0.72 seconds)

 

# Name: SDE_CreateVersion
# Description: Creates a new SDE version

import arcpy, time, smtplib, os
from arcpy import env

arcpy.env.overwriteOutput = True

arcpy.env.workspace = "Database Connections/NEO_1.sde" 
connection = arcpy.env.workspace

# Set local variables
sdeConnection = "Database Connections\NEO_1.sde"
parentVersion = "sde.DEFAULT"
versionName = os.environ.get( "USERNAME" )

fullPath = os.path.join("Windows Credentials: EGAS", versionName)
userVersion = os.path.join("Version Currently Exists: ",versionName)
createVersion = os.path.join("Version Created: ",versionName)

# Clear Workspace Cache
print("Empty Workspace Cache")
arcpy.ClearWorkspaceCache_management() 
print("Done")

# Database Connection: NEO_1 = True  
if arcpy.Exists(connection):  
    versionFound = False
    try:
        for version in arcpy.da.ListVersions(sdeConnection):
            if version.name.split(".")[0] == versionName:
                print(userVersion)
                versionFound = True
                break

        # Execute CreateVersion
        if not versionFound:
            arcpy.CreateVersion_management(sdeConnection, parentVersion, versionName, "PROTECTED")
            print(createVersion)

    except arcpy.ExecuteError:
        print(arcpy.GetMessages())


# Database Connection: NEO_1 = False       
if not arcpy.Exists(connection):
    print("Creating Database Connection: NEO_1")
    arcpy.CreateDatabaseConnection_management(out_folder_path="Database Connections",
                                              out_name="NEO_1.sde",
                                              database_platform="SQL_SERVER",
                                              instance="lan-svr-sql",
                                              account_authentication="OPERATING_SYSTEM_AUTH",
                                              database="NEO_1",
                                              version_type="TRANSACTIONAL",
                                              version="sde.DEFAULT")
    arcpy.env.workspace = r"Database Connections\NEO_1.sde"
    if arcpy.Exists(connection):
        versionFound = False
        try:
            for version in arcpy.da.ListVersions(sdeConnection):
                if version.name.split(".")[0] == versionName:
                    print(existingVersion)
                    versionFound = True
                    break
                #Execute CreateVersion
                if not versionFound:
                    arcpy.CreateVersion_management(sdeConnection, parentVersion, versionName, "PROTECTED")
                    print(createVersion)
                   
        except arcpy.ExecuteError:
            print(arcpy.GetMessages())

Outcomes