Dictionary/Hash question
Gabriel Genellina
gagsl-py at yahoo.com.ar
Tue Feb 6 20:32:47 EST 2007
En Tue, 06 Feb 2007 22:18:07 -0300, Sick Monkey <sickcodemonkey at gmail.com>
escribió:
> I have never seen this "with open(fname,'r') as finput:"
>
> It is actually throwing an error . Do I have to import a special
> library to
> use this?
>
> File "dictNew.py", line 23
> with open(fname,'r') as finput:
> ^
> SyntaxError: invalid syntax
Oh, sorry. You need two things:
- Python 2.5
- include this line at the very beginning of your script: from __future__
import with_statement
If you're using an earlier version, you can write:
finput = open(fname,'r')
try
...
finally
finput.close()
(Or just omit the try/finally and rely on the garbage collector, but it's
not the recommended practice, specially when external resources are
involved, like files).
--
Gabriel Genellina
More information about the Python-list
mailing list