Python 2.5.5 (r255:77872, Nov 28 2010, 19:00:19)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> with open("travellerList.txt", "a") as myfile:
<stdin>:1: Warning: 'with' will become a reserved keyword in Python 2.6
File "<stdin>", line 1
with open("travellerList.txt", "a") as myfile:
^
SyntaxError: invalid syntax
你需要摆脱
myfile.close()
。这很好:with
块将自动关闭块末尾的myfile
。当你试图关闭它自己,它实际上已经超出了范围。你知道吗但是,似乎您使用的是早于2.6的python,其中添加了
with
语句。请尝试升级python,如果无法升级,请使用文件顶部的from __future__ import with_statement
。你知道吗最后一件事,idk replytraveler是什么,但是你把它命名为一个类,它必须是一个字符串才能将它写入一个文件。你知道吗
with
语法仅在python2.6中完全启用。你知道吗必须使用Python 2.5或更早版本:
在Python2.5中使用
from __future__ import with_statement
启用语法:从^{} statement specification :
将文件用作上下文管理器的意义在于它将自动关闭,因此
myfile.close()
调用是多余的。你知道吗对于Python2.4或更早版本,恐怕您运气不好。您必须使用
try
-finally
语句:相关问题 更多 >
编程相关推荐