When I use open()
in python 2.4.3, i get the following error
File "/tmp/chgjdbcconfig.py", line 16 with open("text.new", "wt") as fout: ^ SyntaxError: invalid syntax
I checked the python version and this is my output
Python 2.4.3
I was looking for advice on what could be the alternative. I am trying to edit lines in an XML file on a linux server and I have no control over the upgrades of the python version.
Any advice would be great!
open
isn't missing in 2.4. What's missing is the with
statement (added in Python 2.5 as PEP-0343)
To do the same code without with
:
fout = open("text.new", "wt")
# code working with fout file
fout.close()
Note the fout.close()
is implicit when using with
, but you need to add it yourself without
with
statements have the added benefit of closing the file even if an exception is raised, so a more precise analogue would actually be:
fout = open("text.new", "wt")
try:
# code working with fout file
finally:
fout.close()
it's not open()
it is choking on. It is the with
syntax. Context managers did not exist in Python 2.4.
you must explicitly close the file also.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.