AnsweredAssumed Answered

Arcpy script behaves differently in command prompt and C#

Question asked by stain3565 on Sep 16, 2014

I have an Arcpy script that is having issues with escape backslashes in a folder name when passed in from a C# Process command and this does not happen in command prompt. I run both on the same server so would expect both to work the same when opassed in the same data.

 

The code that is behaving differently is below (apolgies in advance as I cannot find the command to embed source code in a nice way):

 

    message = "[INFO]  GGF - Output_dir initially is " + output_dir + " at " + str(datetime.datetime.now())

    #print message

    arcpy.AddMessage(message)

    win32evtlogutil.ReportEvent(appName,0,0,win32evtlog.EVENTLOG_INFORMATION_TYPE,[message])

    ## workaround to deal with C# putting in extra \ in folder names

    output_dir_frm = output_dir.replace("\\\\", "\\");

    message = "[INFO]  GGF - Output_dir formatted is " + output_dir_frm + " at " + str(datetime.datetime.now())

    #print message

    arcpy.AddMessage(message)

    win32evtlogutil.ReportEvent(appName,0,0,win32evtlog.EVENTLOG_INFORMATION_TYPE,[message])

 

As you can see, the statement in question is in bold. When I call from command prompt, the 3rd argument is the one I am trying to fix:

 

"D:\\TFS\\Pegasus_MWingfield\GenerateGeorouteFilesNetwork.py" "C:\\Users\\mark.r.wingfield\\AppData\\Roaming\\ESRI\\Desktop10.2\\ArcCatalog\

\DWOP.SDE" 3903 "\\\\RMGAPPODC047V\\PegasusTier1DevData$\\alstalba\\Universal\\"

 

This is simply to mimic what is happening in the C# call. In C#, I am using the 32 bit version of Python.exe (the 64 bit doesn't work at all) and the 3rd argument is as follows:

 

"\\\\RMGAPPODC047V\\PegasusTier1DevData$\\alstalba\\Universal\\"

 

Looks the same so far.

 

When running the command prompt version, the message values created are as follows:

 

[INFO]  GGF - Output_dir initially is \\\\RMGAPPODC047V\\PegasusTier1DevData$\\alstalba\\Universal\ at 2014-09-16 14:40:32.242000

[INFO]  GGF - Output_dir formatted is \\RMGAPPODC047V\PegasusTier1DevData$\alstalba\Universal\ at 2014-09-16 14:40:32.244000

 

This is fine as it correctly removes escape values in every occurrence.


When running from a C# Process command:

 

 

[INFO]  GGF - Output_dir initially is \\\\RMGAPPODC047V\\PegasusTier1DevData$\\alstalba\\Universal\" at 2014-09-16 16:03:11.289000

 

[INFO]  GGF - Output_dir formatted is \\RMGAPPODC047V\\PegasusTier1DevData$\\alstalba\\Universal\" at 2014-09-16 16:03:11.290000

 

So, using exactly the same python script (I have no other!!!), this time it seems to treat the "\\\\" as physical backslashes and so only replaces that first set of 4 backslashes. This then means the folder format is invalid and my process fails when trying to use this folder name.

 

It is cleard from the output that the string to be formtted is identical in each case but the result of the arcpy replace is different.

 

Is there any means of printing some information from within the script that will show what version of python.exe is being run as this seems to be the only explanation I can think of.

 

Once again, there is only one version of my script and I can guarantee that this is the version called from the C# (indeed, when it was failing and i needed to identfiy the issue, I put the command in the one script, along with the before and after message, and reran from both environments, thus proving it is the one script.  

Outcomes