473,382 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Question about using "with"

>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
2 1425
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Berthold Hoellmann | last post by:
Hello, When I use ./configure --with-thread --with-fpectl --with-signal-module \ --with-pymalloc --enable-shared --with-cxx=g++ make test on 2.3.3 I get
0
by: Justin | last post by:
Hi. I decided to tidy up some of my queries, but I came across something that stumpt me: mysql> SELECT -> jobs.jobId, -> jobs.active, -> jobs.title, -> jobs.listed, -> ...
14
by: john.burton.email | last post by:
I've done some extensive searching and can't seem to find an answer to this - Is it correct to using "using" with templates, for example: using std::vector; Or do I need to specify the type...
9
by: mahurshi | last post by:
i have a quick question i am putting a debug flag in my program (i really dont need this feature, but i figured it might be useful when i get into trouble) so i want to check if argv is the...
4
by: JMUApache | last post by:
Hi, A have a problem with Inhreit Control. I try to Change its Size in Construct Function, And it works. Code Here: -------------------------------------------------- public class...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
2
by: Phillip Vong | last post by:
VS2005 on a SQL 2000 Please help me understand how to fix this problem. In SQL, a column is assigned Data Type of nvarchar(250). In each row, the value looks like this. FirstName LastName...
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
1
RMWChaos
by: RMWChaos | last post by:
I grabbed this "Rock Solid addEvent" code from this site, which is based on Mark Wubben's event-cache code. (These links for reference only.) I am having two problems with it, and the webmaster is...
3
by: ElBeardo | last post by:
Hello, I´m new to Python.. so this is a newbee question. I´d like to put the value enterd in the entryfield in a variable. I´m trying to build a calculator with python and TKinter, coding it in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.