使用“with open”时出现语法错误在Python中(Python新手)

发布于 2024-08-29 22:08:41 字数 529 浏览 4 评论 0原文

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

宁愿没拥抱 2024-09-05 22:08:41

你所拥有的应该是正确的。 Python 2.5 引入了 with 语句,您可以从 __future__ 导入。既然你的代码是正确的,我能想到的唯一解释是你的python版本不是你想象的那样。您很有可能在系统上安装了多个版本的 python,并且由于某种原因您的代码正在使用旧版本运行。尝试像这样运行它:

[root@234571-app2 git]# /usr/bin/python2.5 test.py

假设这有效,您可以更改第一行以指示您想要的 python 版本。它可以是 python2.5 的直接路径,也可以使用 env 命令在用户的 PATH 变量中搜索 python2.5。正确的方法取决于您的系统 python 安装的内容。以下是两种方法:

要直接使用 /usr/bin/python2.5,您可以执行以下操作:

#!/usr/bin/python2.5

要使用 PATH 中首先出现的 python2.5 版本,请执行以下操作:

#!/usr/bin/env 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:

[root@234571-app2 git]# /usr/bin/python2.5 test.py

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 the env command to search the user's PATH 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:

#!/usr/bin/python2.5

To use whichever version of python2.5 occurs first in your PATH, do this:

#!/usr/bin/env python2.5
疏忽 2024-09-05 22:08:41

也许像这样?

#!/usr/bin/env python2.5
from __future__ import with_statement

with open("/home/git/post-receive-email.log",'a') as log_file:
    log_file.write("hello world")

Maybe like this?

#!/usr/bin/env python2.5
from __future__ import with_statement

with open("/home/git/post-receive-email.log",'a') as log_file:
    log_file.write("hello world")
野心澎湃 2024-09-05 22:08:41

这个问题的答案隐藏在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 the env 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文