Got Error when run Window Task Scheduler - Python: Last Run Result (0x2)

1304
8
Jump to solution
03-15-2023 01:39 PM
AlexP_
by
Occasional Contributor III
Hello all,
 

I have ArcGIS Pro 3.0, window task scheduler and python 27. I am not expertise with window task scheduler. Please see screenshots and error below. It is working successful when I run python script manually. But when I tried to run window task scheduler, I got error messages. I am not sure what to do. Please kindly assist. Thank you so much in advance.

AlexP__4-1678912359486.png

 

AlexP__3-1678912277055.png

 

ERROR MESSAGES

AlexP__1-1678912026620.pngAlexP__1-1678912944089.png

 

AlexP__0-1678911899047.png

 

AlexP__0-1678912900829.png

 

PYTHON SCRIPT CODE

 

 

 

import arcpy
import csv
import os
from datetime import date
from datetime import datetime

matchTableName = r"ElevationCertificatesAttachmentMatchTable.csv"
docFolder = r"Q:\2023\IMPORTED TO GIS"

matchField = "MatchID"
pathField = "Filename" 

#change this date to the disired value
processFilesAddedOnDate = date.today()

# iterate through each .pdf file in the directory and write a row to the table

       
for (root, dirs, files) in os.walk(docFolder):
    workingFolder = root
    matchTableNamePath = os.path.join(root, matchTableName)
    # create a new Match Table csv file
    if os.path.exists(matchTableNamePath):
        os.remove(matchTableNamePath)
    writer = csv.writer(open(matchTableNamePath, "w", newline=''), delimiter=",")
    # write a header row (the table will have two columns: ParcelID and Picture)
    writer.writerow([matchField, pathField, 'fullPath'])
    for f in files:
        
        if '.pdf' in f:
            #if datetime.strptime(os.path.getctime(os.path.join(root, f)),"%m/%d/%Y") >= processFilesAddedOnDate:
            if date.fromtimestamp(os.path.getctime(os.path.join(root, f))) >= processFilesAddedOnDate:
                
                #print(processFilesAddedOnDate)
                #print(date.fromtimestamp(os.path.getctime(os.path.join(root, f))))
                fileNameToAddress = f.replace(".pdf", "")
                fileNameToAddress = fileNameToAddress.replace("  ", " ");
                fileNameToAddress = fileNameToAddress.replace("nw", "NW")
                try:
                    firstPartUntilNW = fileNameToAddress.split("NW")[0].strip().split(" ")
                    restAfterNW = fileNameToAddress.split("NW")[1].strip()
                    #print(firstPartUntilNW, len(firstPartUntilNW))
                    #print(restAfterNW)
                    fileNameToAddressWithNoPermitNumber = ""
                    if len(firstPartUntilNW) == 2:
                        fileNameToAddressWithNoPermitNumber = firstPartUntilNW[1]+" NW "+restAfterNW
                    if len(firstPartUntilNW)== 1:
                        fileNameToAddressWithNoPermitNumber = firstPartUntilNW[0]+" NW "+restAfterNW
                    #print(fileNameToAddressWithNoPermitNumber)
                    writer.writerow((fileNameToAddressWithNoPermitNumber,f, os.path.join(root, f)))
                except:
                    continue
      
    del writer

    # Set the workspace environment and run add attachment
    arcpy.env.workspace = r'C:\Users\AppData\Roaming\Esri\ArcGISPro\Favorites\test.sde'

    arcpy.management.AddAttachments('sde.TEST.TEST', 'ADDR', matchTableNamePath, "MatchID", "Filename", workingFolder)

 

 

0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Frequent Contributor

I see you have it set to run whether user logged in or not, so possibly running scheduled task when not logged in.

Not sure if this is your issue, but may be related to this line:

 

docFolder = r"Q:\2023\IMPORTED TO GIS"

 

Only the local drives (C:\) exist when no user is logged in.  The mapped drives are user specific, so when nobody is logged in, the Q: drive doesn't exist.

Try using the UNC paths for everything other than local drives and see if it helps.

Also, unless you specifically created it, don't think this path would be valid either:

arcpy.env.workspace = r'C:\Users\AppData\Roaming\Esri\ArcGISPro\Favorites\test.sde'

Normally, would have a username in there:

arcpy.env.workspace = r'C:\Users\UserName\AppData\Roaming\Esri\ArcGISPro\Favorites\test.sde'

 

R_

View solution in original post

8 Replies
MichaelVolz
Esteemed Contributor

Python 2.7 works with ArcMap and python 3.x (depends on Pro version) works with Pro.  I do not think this is the issue as the path that I can see looks like it is for Pro python.

Was this code originally run for ArcMap and you are trying to migrate it to Pro?

0 Kudos
Mahdi_Ch
New Contributor III

Hi Alex,

I never used WinTS myself but just got curious and checked your screenshots and did not see any explicit error message, except for the one that returned code 2147942402. So, I looked that up and apparently it has something to do with the path of the executable that you pass. Based on what I read you need to make sure all the paths really exist and there is no typo there. In "C:\Program Files\ArcGIS\Pro\bin\Python\envs\... " or the part 'add arguments (optional)' that you wrote your script's name. Make sure to enter the full path to your script there. 

Check this link. Not the exact problem but might worth checking. Sorry that's all I've got for now. Hopefully you will find an answer soon. 

 

0 Kudos
AlexP_
by
Occasional Contributor III

Hello,

Thank you for your reply. the add argument is already add full path. by full path, I have different letter than C: and it is already set up map network. I am gonna try using UNC as per @RhettZufelt .

0 Kudos
RhettZufelt
MVP Frequent Contributor

I see you have it set to run whether user logged in or not, so possibly running scheduled task when not logged in.

Not sure if this is your issue, but may be related to this line:

 

docFolder = r"Q:\2023\IMPORTED TO GIS"

 

Only the local drives (C:\) exist when no user is logged in.  The mapped drives are user specific, so when nobody is logged in, the Q: drive doesn't exist.

Try using the UNC paths for everything other than local drives and see if it helps.

Also, unless you specifically created it, don't think this path would be valid either:

arcpy.env.workspace = r'C:\Users\AppData\Roaming\Esri\ArcGISPro\Favorites\test.sde'

Normally, would have a username in there:

arcpy.env.workspace = r'C:\Users\UserName\AppData\Roaming\Esri\ArcGISPro\Favorites\test.sde'

 

R_

AlexP_
by
Occasional Contributor III

Hello @RhettZufelt Thank you for your reply. Do I also need to input UNC path on add argument optional since it is not letter C:?

0 Kudos
RhettZufelt
MVP Frequent Contributor

"ANY" paths that are not on the local drive will need to be UNC for it to run as scheduled task when user not logged in.

R_

Mahdi_Ch
New Contributor III

@RhettZufelt 

Please correct me if I am wrong, but I thought when installing Pro for the machine (all users), that puts the installed directory in the ProgramFiles and when installing in "User Only" mode, that will be created under the User directory. 

0 Kudos
RhettZufelt
MVP Frequent Contributor

I don't see reference to ProgramFiles in the script.  However, I have only installed for the machine (all users), it then keeps a separate app settings folder (hidden) for each user logged in when they run Pro like so:

C:\Users\UserName\AppData\Roaming\Esri\ArcGISPro\Favorites

I've never tried "User Only" mode, so not sure how/where it stores the app setting in this case.

You should be able to check your computer and see if there is an AppData folder with Pro settings in the root level of the users directory, or if you need to open specific user folder first.

C:\Users\AppData\Roaming\Esri\ArcGISPro\Favorites

R_

0 Kudos