Question about using "with"

Steven W. Orr steveo at syslang.net
Tue Jan 9 13:23:49 EST 2007


>From the tutorial, they said that the following construct will 
automatically close a previously open file descriptor:

-------------------
#! /usr/bin/python
import sys

for nn in range ( 1, len(sys.argv ) ):
     print "arg ", nn, "value = ", sys.argv[nn]
     with open(sys.argv[nn]) as f:
         for line in f:
             print line,
------------------

but when I run it (with args) I get:

591 > ./cat.py cat.py
   File "./cat.py", line 6
     with open(sys.argv[nn]) as f:
             ^
SyntaxError: invalid syntax
592 >

This example came from http://docs.python.org/tut/node10.html down in 
section 8.7

Am I missing something?

TIA

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net



More information about the Python-list mailing list