print 1/0 try: print 1/0 except ZeroDivisionError: print 'Error trying to divide by zero.' import traceback # Try to get a file name try: fn = raw_input('Nome do arquivo: ').strip() # Numbering lines for i, s in enumerate(file(fn)): print i + 1, s, # If an error happens except: # Show it on the screen trace = traceback.format_exc() # And save it on a file print 'An error happened:\n', trace file('trace.log', 'a').write(trace) # end the program raise SystemExit import random # Creates a file with 25 random numbers with file('temp.txt', 'w') as temp: for y in range(5): for x in range(5): # "print >> " records command output on the file print >> temp, '%.2f' % random.random(), print >> temp # Shows file content with file('temp.txt') as temp: for i in temp: print i, # Out of the blocks, the file will be closed # The following command generates an exception ValueError: I/O operation on closed file print >> temp