我为 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
现在,当我尝试运行脚本时出现此错误:
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
谢谢, 罗恩。
最佳答案
在 Python 2.5 中,您实际上可以使用 with
语句——只需从 __future__
导入它:
from __future__ import with_statement
关于Python 2.5.2-代替 'with' 语句的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7918745/