This may help - I sometimes run into issues depending with printing when migrating scripts between 'run in ArcGIS python console', 'run in a Toolbox tool', and 'run in a stand-alone (CMD shell or IDLE) mode'. This one was developed to run a log file (called fBad - because it's an error log). So, read in your command arguments or over-ride and explictly set the path the log file:
if arcpy.GetParameterAsText(2):
logBadData = arcpy.GetParameterAsText(2)
else:
logBadData = "R:\\test_pGDB"
###
# Create Output Files
###
#output log Bad Data
timeStamp = "2012_07_10" # or whatever, use a function to stamp it!
fBad = open(logBadData+"_"+timeStamp+".txt",'w')
And now pass your error messages to the function
def tbxPrint(fBad,inStr):
'''a general purpose function for try/except printing
inside a toolbox function'''
try:
arcpy.AddMessage(inStr)
except:
print inStr
finally:
fBad.write(inStr+"\n")
in a manner like
tbxPrint(fBad,"\n \n \n Created "+PathAndNameGDB)
tbxPrint(fBad,"\n with log file "+logBadData+"\n \n ")
To save 5 characters you could use default arguments if you only had 1 log file, but I didn't, so there it is. Just remember to close your file (or file-like object) at the end of the script as good practice.Daryl