open()的Python语法错误

open()的Python语法错误,python,Python,辅导 我的代码, 在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

辅导

我的代码,
在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
~$~/sys/Python-2.5.6/Python
Python 2.5.6(r256:88840,2012年7月12日,12:21:58)
[GCC 4.6.3]关于linux3
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>以开放(“fred”)作为f:
:1:警告:“with”将成为Python 2.6中的保留关键字
文件“”,第1行
以开放(“fred”)作为f:
^
SyntaxError:无效语法

添加
import sys
print sys.version
以查看您正在使用的真实版本。

没有,因此代码的不同部分出现问题。您可以再试一次吗?我觉得很好,你能给我看一些关于这句话的代码吗?@Puciek我就是这么想的。我不知道为什么它会引起错误?我正在使用-Wd运行,这会影响它吗?发布运行此文件的整个日志,以及python--version如果是这种情况,您可以使用
from\uuuuuuu future\uuuuu导入with\u语句
。不过最好升级Python版本。
~$ ~/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