使用“with open”时出现语法错误在Python中(Python新手)
[root@234571-app2 git]# ./test.py
File "./test.py", line 4
with open("/home/git/post-receive-email.log",'a') as log_file:
^
SyntaxError: invalid syntax
代码如下所示:
[root@234571-app2 git]# more test.py
#!/usr/bin/python
from __future__ import with_statement
with open("/home/git/post-receive-email.log",'a') as log_file:
log_file.write("hello world")
我正在使用 Python 2.5.5
[root@234571-app2 git]# python -V
Python 2.5.5
[root@234571-app2 git]# ./test.py
File "./test.py", line 4
with open("/home/git/post-receive-email.log",'a') as log_file:
^
SyntaxError: invalid syntax
The code looks like this:
[root@234571-app2 git]# more test.py
#!/usr/bin/python
from __future__ import with_statement
with open("/home/git/post-receive-email.log",'a') as log_file:
log_file.write("hello world")
and I am using Python 2.5.5
[root@234571-app2 git]# python -V
Python 2.5.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你所拥有的应该是正确的。 Python 2.5 引入了 with 语句,您可以从 __future__ 导入。既然你的代码是正确的,我能想到的唯一解释是你的python版本不是你想象的那样。您很有可能在系统上安装了多个版本的 python,并且由于某种原因您的代码正在使用旧版本运行。尝试像这样运行它:
假设这有效,您可以更改第一行以指示您想要的 python 版本。它可以是
python2.5
的直接路径,也可以使用env
命令在用户的PATH
变量中搜索 python2.5。正确的方法取决于您的系统 python 安装的内容。以下是两种方法:要直接使用 /usr/bin/python2.5,您可以执行以下操作:
要使用 PATH 中首先出现的 python2.5 版本,请执行以下操作:
What you have should be correct. Python 2.5 introduced the with statement as something you can import from
__future__
. Since your code is correct, the only explanation I can think of is that your python version is not what you think it is. There's a good chance you have multiple versions of python installed on the system and for some reason your code is running with an older version. Try running it like this:Assuming this works, you can change your first line to indicate which version of python you'd like. That can either be a direct path to
python2.5
or you can use theenv
command to search the user'sPATH
variable for python2.5. The correct approach depends on what your systems python installs are. Here are the 2 approaches:To use /usr/bin/python2.5 directly, you can do this:
To use whichever version of python2.5 occurs first in your PATH, do this:
也许像这样?
Maybe like this?
这个问题的答案隐藏在OP的评论中。一旦 @Tony 确认他的代码正在由 2.4 执行,@Tamas 就给出了上面的正确解决方案:
“所以,
/usr/local/bin/python
是 2.5.5,但是您正在使用以下命令调用脚本/usr/bin/python
是 2.4.3。尝试将 shell shebang 行替换为:#!/usr/bin/env python
。”一般来说,要小心对路径进行硬编码,即
/usr/bin
、/usr/local/bin
等。这就是env
的原因code> 命令被发明。当您的系统上安装了多个版本的 Python 时,这一点尤其重要。然而,这是一个非常明显的泄露,这是一个古老的 Python 问题,因为 OP 代码将在任何 2.5 及更新版本的解释器上执行。无论您认为您使用的是哪个版本的 Python,该语法错误都会发出此消息。
the answer to this question is buried in the comments of the OP. @Tamas gave the correct solution above once @Tony confirmed that his code was being executed by 2.4:
"So,
/usr/local/bin/python
is 2.5.5, but you are calling your script with/usr/bin/python
which is 2.4.3. Try replacing the shell shebang line with this:#!/usr/bin/env python
."in general, be wary of hardcoding your path, i.e.,
/usr/bin
,/usr/local/bin
, etc. this is why theenv
command was invented. it's especially relevant when you have multiple versions of Python installed on your system.however, it was a pretty clear giveaway that it was an old Python issue as that OP code will execute on any 2.5 and newer interpreter. that syntax error gives off this message regardless of what version of Python you think you're using.