python - Saving an error exceptions to file erroring out -


i'm having issues simple bit of code not working correctly , i'm totally baffled why...

errors = open('error(s).txt', 'w')  try:      execfile("aeaper.py") except exception e:     errors.write(e)  errors.close() 

i following error:

traceback (most recent call last):   file "c:\sator.py", line 45, in <module>     periodically(2, -1, +1, callscripts)   file "c:\sator.py", line 27, in periodically     s.run()   file "c:\python27\lib\sched.py", line 117, in run     action(*argument)   file "c:\sator.py", line 36, in callscripts     errors.write(e) typeerror: expected character buffer object 

what wrong code , why doing that?

unlike print statement, file.write function takes strings. so, need convert string explicitly:

errors.write(str(e)) 

of course in real-life code, want formatting, forget this. example:

errors.write('failed exec {} {}'.format(filename, e)) 

here, we're passing result of format write, fine, , we're passing e argument format, fine… the fact we've done implicit conversion string in middle easy miss.

there 2 different ways represent value string, str , repr, str 1 print uses, it's wanted here.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -