AnsweredAssumed Answered

Is there a simple way to use sys.stdout.write with arcpy.AddMessage?

Question asked by rastrauch Champion on Aug 16, 2016
Latest reply on Oct 20, 2016 by rastrauch

I'm using

sys.stdout.write( "." )

in a a loop to print a series of dots when it has processed an .mxd that didn't fit the criteria that I am working on (if it fits criteria, then it lists the path).  It's just a very simple "progress bar" for my tool.

 

This works fine in a python window, but I'm not sure how to do this in a tool, without getting a bunch of newlines that come with a print or arcpy.AddMessage (which looks messy...but I could live with it)

 

I've read ab out many of the progress bar approaches, but this one line (or equivalent) is what I'm hoping for.  I'll include a snippet of the code..

 

# ...  snippet....will not run as standalone as is
updateServices = []
update = False
myMsgs("\nFinding STARTED services accesing {0} to STOP\n   Dots represent services that are OK".format(updateDS[0]))
for startedService in myServices:
     mxdFound = False
     if not update:  # will print a dot for services or df within mxd that are ok, using for progress status
          sys.stdout.write( "." )
     update = False
     #myMsgs("Reviewing service: {0}".format(startedService))
     theServicePath = os.path.join(servicesDir, startedService)
     if not arcpy.Exists(theServicePath):
          myMsgs("...we have a problem")
          exit
     else:
          # myMsgs(" {0}".format(startedService))  # Can use if want to list all services
          for root, dirs, files in os.walk(theServicePath):

               for fn in files:
                    fullPath = os.path.join(root, fn)
                    basename, extension = os.path.splitext(fn)
                    if not extension == ".mxd":
                         pass
                    else:
                         #myMsgs("  mxd file found")
                         mxd = arcpy.mapping.MapDocument(fullPath)
                         try: 
                              # Do stuff with MXD using arcpy.mapping 
                              for df in arcpy.mapping.ListDataFrames(mxd):
                                   lyrList = arcpy.mapping.ListLayers(mxd)
                                   mxdFound = True
                                   for lyr in lyrList:
                                        if lyr.isFeatureLayer and update == False:
                                             if lyr.supports("dataSource") and update == False:
                                                  theSource = lyr.dataSource
                                                  if updateDS[0] in theSource:
                                                       update = True
                                                       myMsgs(" {0} is using {1}".format(startedService, updateDS[0]))     
                                                       updateServices.append(startedService)
                                                       break

                         except AttributeError: 
                              myMsgs("\n   !!! WARNING: {0} has unreadable dataframes(s),\n       will stop as precaution...\n".format(os.path.basename(mxd.filePath)))
                              updateServices.append(startedService)
                              update = True
                              pass                                
                         del mxd
# ...

 

Line #8 is where the line is that works in the python window.

 

Thanks.

Outcomes