A place for my personal musings, about programming, design or whatever come across my mind

Exception handling in python

As I came across the exception handling chapter, both code below should work as stated in the book (The Head First Python book, which is indented for Python 3.X, but I am running this code in Python 2.X),

try:
  with open("man_data.txt") as man_file:
    print >>man_file, man
  with open("other_data") as other_file:
    print >>other_file, other

or

try:
  with open("man_data.txt") as man_file, open("other_data") as other_file:
    print >>man_file, man
    print >>other_file, other

However it seems only the first one is working under Python 2.X, later code will in fact throw up a syntax error message.
Didn’t see anything on the net mentioning this has to do with changes in Python 3.x, so not sure if that is the case.

1 Ping/Trackback
<