spir wrote:
Thank you, Tim, this really answers my question.
Glad to be of help :)
* I intended to write a helper func "filetext(filename)" to
open/read/close/return (or equivalent using the with idiom).
This is one of those tricky things in Python: when the "raw" code
is possibly one lin
Le Thu, 11 Jun 2009 10:21:31 +0100,
Tim Golden s'exprima ainsi:
> spir wrote:
> > Hello,
> >
> > text = file(filename).read()
> >
> > (or: open(filename).read())
> >
> > How do you close() the file? ;-)
>
> Well, in any version of Python, you can do this:
>
>
> f = open (filename)
> text =
spir wrote:
Hello,
text = file(filename).read()
(or: open(filename).read())
How do you close() the file? ;-)
Well, in any version of Python, you can do this:
f = open (filename)
text = f.read ()
f.close ()
or, slightly more robsustly:
f = open (filename)
try:
text = f.read ()
finally:
Hello,
text = file(filename).read()
(or: open(filename).read())
How do you close() the file? ;-)
Is it, in fact, automagically closed after end-of-statement if not bound to a
name? Or when the execution path quits the present scope (func, module), like
if it had been bound to a local var? Or w