AnsweredAssumed Answered

A way to loop through print outputs and write them to a file?

Question asked by kjdowdy4026 on Jan 8, 2015
Latest reply on Jan 8, 2015 by kjdowdy4026

I have this code, same code I posted yesterday but different piece.  In 10.2.2 I am trying to get the script to write all print outputs to a text file.  Code block in question starts at line 18.


import arcpy, os, sys  arcpy.env.workspace = arcpy.GetParameterAsText(0)  def get_size(start_path = arcpy.env.workspace):     total_size = 0     for dirpath, dirnames, filenames in os.walk(start_path):         for f in filenames:             fp = os.path.join(dirpath, f)             total_size += os.path.getsize(fp)     return round((total_size / float(1048576)), 2) print "Total Size before Compact " + str(get_size()) + "mb" results = open("R:\\results.txt","w") results.write("Total Size before Compact " + str(get_size()) + "mb" + '\n')  feature_classes = []  for dirpath, dirnames, filenames in arcpy.da.Walk(arcpy.env.workspace, datatype="Container"):     for dirname in dirnames:         if ".gdb" in dirname:             arcpy.Compact_management(os.path.join(dirpath, dirname))             print "Successfully compacted " + dirname results.write("Successfully compacted " + dirname + '\n') results.close()


There are hundreds of GDBs in this workspace.  In the ArcMap console, it returns each GDB it finds and prints "Successfully compacted this.gdb".  However, when I try to write the print to a file it only writes the last GDB in the list.  How do I get it to list every GDB it successfully compacts in the text file? Thanks!