Question about using "with"
Steven Bethard
steven.bethard at gmail.com
Tue Jan 9 13:39:59 EST 2007
More information about the Python-list mailing list
Tue Jan 9 13:39:59 EST 2007
- Previous message (by thread): Question about using "with"
- Next message (by thread): Question about using "with"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Steven W. Orr wrote: >> 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? You need to enable the with statement using a __future__ import:: >>> with open('temp.txt', 'w') as f: <stdin>:1: Warning: 'with' will become a reserved keyword in Python 2.6 File "<stdin>", line 1 with open('temp.txt', 'w') as f: ^ SyntaxError: invalid syntax >>> from __future__ import with_statement >>> with open('temp.txt', 'w') as f: ... f.write('hello') ... STeVe
- Previous message (by thread): Question about using "with"
- Next message (by thread): Question about using "with"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list