我有以下内容:
with open("c:\xml1.txt","r") as f1, open('c:\somefile.txt','w') as f2:
Run Code Online (Sandbox Code Playgroud)
这是一个语法错误:
with open("c:\xml1.txt","r") as f1, open('c:\somefile.txt','w') as f2:
^
SyntaxError: mismatched input ',' expecting COLON
Run Code Online (Sandbox Code Playgroud)
我正在使用netbeans python插件,它依赖于jython 2.5.1
我已经添加了:
from __future__ import with_statement
Run Code Online (Sandbox Code Playgroud)
但这并没有改变任何事情.
关于该怎么做的任何建议?
谢谢
多个上下文管理器的语句只在python2.7中添加,请参阅文档.
对于jython2.5,您需要from __future__ import with_statement启用单上下文管理器功能.
编辑:
有趣的是,甚至jython2.7b2都不支持多个上下文管理器.
你可以做的是嵌套上下文:
with open("c:/whatever") as one_file:
with open("c:/otherlocation") as other_file:
pass # or do things
Run Code Online (Sandbox Code Playgroud)