Select to view content in your preferred language

Geoprocessing Tool prevents printing in Python

1177
6
04-27-2010 10:57 AM
JoeHeggenstaller
Regular Contributor
When using geoprocessing tools in a Python (2.5) script it appears to be preventing the script from outputting any further "print" command text to the screen. I am using various gp tools and they all appear to exhibit the same behavior. I use python "write" commands to write script processing progress into a text log, which is still working correctly. An example would be where I'm using the gp.FeatureClassToShapefile_conversion tool to export shapefiles from a geodatabase to a folder. If I comment out the gp tool the While loop proceeds perfectly and will print to the screen and write to the log. Here is a snippet of the code (it is properly indented, but won't display properly in this post):

# Sample Variables
New_Geo_GDB = "\\\\c:\\test\\ blahblah.gdb


try:
print "Start exporting shapefiles from feature classes of " + New_Geo_GDB + " " + time.strftime("%H:%M:%S", time.localtime())
writeLog.write('Start exporting shapefiles from feature classes of ' + New_Geo_GDB + ' ' + time.strftime("%H:%M:%S", time.localtime()) + ' \n')
# SET WORKSPACE TO THE GEODATABASE
gp.Workspace = New_Geo_GDB
# CREATE OBJECT TO HOLD GEODATABASE OBJECTS
desc = gp.Describe(New_Geo_GDB)
# CREATE CHILDREN VARIABLE TO HOLD LIST OF FEATURE CLASS NAMES
children = desc.Children
# RESET THE LIST TO THE BEGINNING
children.Reset()
# MOVE TO THE FIRST OBJECT IN THE LIST
child = children.Next()
# LOOP THORUGH THE CHILD OBJECTS WHILE THEY EXIST
while child:
  # CREATE VARIABLE TO STORE THE GEODATABASE AND THE FEATURE CLASS NAME TO EXPORT
  InGeo_Dataset = New_Geo_GDB + "\\" + child.Name
  # PRINT AND WRITE TO THE LOG
  print "Exporting to shapefile folder: " + child.Name
  writeLog.write('Exporting to shapefile folder: ' + child.Name + ' \n')
  # GEOPROCESSING OBJECT TO EXPORT FEATURE CLASS TO SHAPEFILE
  gp.FeatureClassToShapefile_conversion(InGeo_Dataset, shapeDir)
               # MOVE TO THE NEXT FEATURE CLASS
  child = children.Next()
print "Finished exporting feature classes to shapefiles: " + time.strftime("%H:%M:%S", time.localtime())
print " "
writeLog.write('Finished exporting feature classes to shapefiles: ' + time.strftime("%H:%M:%S", time.localtime()) + ' \n')
writeLog.write('  \n')
except:
print "! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! "
print "Export shapefiles failed!"
print gp.GetMessages(2)
writeLog.write('! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !  \n')
writeLog.write('Export shapefiles failed! \n')
writeLog.write(gp.GetMessages(2) + ' \n')
0 Kudos
6 Replies
JamesGonsoski
Occasional Contributor
http://resources.arcgis.com/content/nimbus-bug?bugID=TklNMDQwNTI4

Then wait patiently for version 10.0.

Jim G.
MN Dept of Agriculture
0 Kudos
JoeHeggenstaller
Regular Contributor
Thanks Jim. I looked all over the old & new forums and didn't find that one.
0 Kudos
RDHarles
Regular Contributor
This also (randomly) happens with the gp.FeatureClassToGeodatabase() tool.
0 Kudos
JoeHeggenstaller
Regular Contributor
I noticed that also, but was trying to keep my example simple. Thanks.
0 Kudos
FrankPerks
Emerging Contributor
Hi,

You can overload the sys streams after esri plays with them, to restore print functionality. So you can have a happy print.

import arcgisscripting
import sys

class GPPrint(object):
    """
        Use enable or disable print please =)
    """
    def __init__(self, gp, is_stderr = False):
        """
            Initializer
        """
        self.gp = gp
        self.is_stderr = is_stderr

    def __del__(self):
        self.gp = None

    def write(self, msg):
        if msg == '\n':
            return
        if self.gp:
            if not self.is_stderr:
                self.gp.addmessage(msg)
            else:
                self.gp.adderror(msg)

    def flush(self):
        pass

    def writelines(self, msg):
        self.write('\n'.join(msg))

    def close(self):
        pass

gp = arcgisscripting.create()
sys.stderr = GPPrint(gp, True)
sys.stdout = GPPrint(gp, False)

print "Hi"
print "Hello normally this would not work but i am =)"


# Now lets say we want to restore the streams...

sys.stderr = sys.__stderr__
sys.stdout = sys.__stdout__


However i strongly suggest you look into the logging module for working with large projects.
0 Kudos
JasonScheirer
Esri Alum
I second the use of the Python logging module for any large/long-running tool. It's fairly trivial to add logging handlers that post to gp.addMessage and gp.addError when the severity level of a logging message is high enough, and you can log less important messages to log files for later inspection, etc.
0 Kudos