Python 2.5.2-什么代替了“with”?陈述

发布于 2024-12-12 12:53:51 字数 545 浏览 4 评论 0原文

我为 python 2.7 编写了代码,但服务器有 2.5。我如何重写下一个代码,以便它将在 python 2.5.2 中运行:

gzipHandler = gzip.open(gzipFile)

try:
    with open(txtFile, 'w') as out:
        for line in gzipHandler:
            out.write(line)
except: 
    pass

现在,当我尝试运行我的脚本时,我收到此错误:

警告:“with”将成为 Python 2.6 Traceback 中的保留关键字 (最近一次调用最后一次):文件“Main.py”,第 7 行,位于 从提取器导入提取器文件“/data/client/scripts/Extractor.py”,第 29 行 以 open(self._logFile, 'w') 为例: ^ SyntaxError:语法无效

谢谢, 罗恩.

I wrote my code for python 2.7 but the server has 2.5. How do i rewrite the next code so it will run in python 2.5.2:

gzipHandler = gzip.open(gzipFile)

try:
    with open(txtFile, 'w') as out:
        for line in gzipHandler:
            out.write(line)
except: 
    pass

Right now, when i try to run my script I get this error:

Warning: 'with' will become a reserved keyword in Python 2.6 Traceback
(most recent call last): File "Main.py", line 7, in
from Extractor import Extractor File "/data/client/scripts/Extractor.py", line 29
with open(self._logFile, 'w') as out:
^ SyntaxError: invalid syntax

Thanks,
Ron.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

z祗昰~ 2024-12-19 12:53:52

在Python 2.5中,您实际上可以使用with语句——只需从__future__导入它:

from __future__ import with_statement

In Python 2.5, you actually can use the with statement -- just import it from __future__:

from __future__ import with_statement
你与清晨阳光 2024-12-19 12:53:52

如果不能或不想使用 with,则使用 finally

gzipHandler = gzip.open(gzipFile)
out = open(txtFile, 'w')
try:
    for line in gzipHandler:
        out.write(line)
finally:
    out.close()
    gzipHandler.close()

finally 子句中的清理代码将始终是执行,无论是否引发异常。

If you can't, or don't want to use with, then use finally:

gzipHandler = gzip.open(gzipFile)
out = open(txtFile, 'w')
try:
    for line in gzipHandler:
        out.write(line)
finally:
    out.close()
    gzipHandler.close()

The cleanup code in the finally clause will always be excecuted, whether an exception is raised, or not.

缺⑴份安定 2024-12-19 12:53:52

try/ except 块中代码的“旧”版本将是:

out = open(txtFile, 'w')
for line in gzipHandler:
    out.write(line)
out.close()

with open() ... 上下文管理器在这里实际上是同样的事情。当文件的对象被垃圾收集时,Python 会自动关闭文件(请参阅问题 575278 了解详细信息),因此当 out 所在的函数由于某种原因停止执行时,out 将被关闭。此外,如果 Python 进程在执行 out.close() 之前由于某种原因发生灾难性失败,操作系统将在 Python 进程终止时关闭该文件。

with open() 上下文管理器将扩展为大约:

out = open(txtFile, 'w')
try:
    for line in gzipHandler:
        out.write(line)
finally:
    out.close()

有关说明,请参阅上面的“上下文管理器”链接。那么它是如何运作的呢?它打开文件,执行代码块,然后显式关闭文件。我描述的“旧”版本如何工作?它打开文件,执行代码块,然后在文件作用域结束或 Python 进程终止时隐式关闭文件。

但对于“显式”与“隐式”部分,功能是相同的。

The "old" version of the code inside your try/except block would be:

out = open(txtFile, 'w')
for line in gzipHandler:
    out.write(line)
out.close()

The with open() ... context manager is effectively the same thing here. Python closes files automatically when their objects are garbage collected (see question 575278 for details), so out will be closed when the function it's in stops executing for some reason. Furthermore, the OS will close the file when the Python process terminates should it fail catastrophically for some reason before out.close() gets executed.

The with open() context manager will expand to approximately:

out = open(txtFile, 'w')
try:
    for line in gzipHandler:
        out.write(line)
finally:
    out.close()

See the above link to "context manager" for an explanation. So how does it work? It opens the file, executes your block of code, then explicitly closes the file. How does the "old" version I describe work? It opens the file, executes your block of code, then implicitly closes the file when its scope is finished or when the Python process terminates.

Save but for the "explicit" vs "implicit" parts, the functionality is identical.

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