教程
http://docs.python.org/2/tutorial/inputoutput.html
>>> with open('workfile', 'r') as f:
... read_data = f.read()
>>> f.closed
True
我的代码,
在 python 2.7.5 中
with open(filea, 'r') as f:
^ SyntaxError: invalid syntax
为什么这是语法错误?
教程
http://docs.python.org/2/tutorial/inputoutput.html
>>> with open('workfile', 'r') as f:
... read_data = f.read()
>>> f.closed
True
我的代码,
在 python 2.7.5 中
with open(filea, 'r') as f:
^ SyntaxError: invalid syntax
为什么这是语法错误?
您没有在 2.7.5 中运行您的代码;你在更早的版本中运行它,可能是 2.4 或 2.5。
~$ ~/sys/Python-2.5.6/python
Python 2.5.6 (r256:88840, Jul 12 2012, 12:21:58)
[GCC 4.6.3] on linux3
Type "help", "copyright", "credits" or "license" for more information.
>>> with open("fred") as f:
<stdin>:1: Warning: 'with' will become a reserved keyword in Python 2.6
File "<stdin>", line 1
with open("fred") as f:
^
SyntaxError: invalid syntax
添加import sys并print sys.version查看您正在使用的真实版本。