By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,842 Members | 1,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,842 IT Pros & Developers. It's quick & easy.

Question about using "with"

P: n/a
>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
Jan 9 '07 #1
Share this Question
Share on Google+
2 Replies

P: n/a
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
Jan 9 '07 #2

P: n/a
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?
The with-statement only works in Python 2.5, and you have to enable it
explicitly:
>>from __future__ import with_statement
with open("tmp.txt") as f:
.... print len(f.read())
....
15
>>f.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: I/O operation on closed file

Peter
Jan 9 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.