2013-05-15 35 views
4

我有以下幾點:與關鍵字和Jython 2.5.1

with open("c:\xml1.txt","r") as f1, open('c:\somefile.txt','w') as f2: 

這是給一個語法錯誤:

with open("c:\xml1.txt","r") as f1, open('c:\somefile.txt','w') as f2: 
            ^
SyntaxError: mismatched input ',' expecting COLON 

我使用NetBeans插件蟒取決於Jython的2.5。 1

我已經加入:

from __future__ import with_statement 

但這並沒有改變任何東西。

任何建議怎麼辦?

謝謝

回答

6

只在python2.7增加了對多個上下文經理聲明,請參閱the documentation

對於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 
+0

有沒有解決辦法? – user61629

+0

這是否解決了您的問題? –

+0

謝謝,解決了它。 – user61629

0

在您的文件路徑,你有一對夫婦的地方「\」,\ x被普遍用於表示十六進制字符。嘗試使用帶「r」的原始字符串或用另一個反斜槓轉義您的反斜槓。

with open(r"c:\xml1.txt","r") as f1, open(r'c:\somefile.txt','w') as f2: 

with open("c:\\xml1.txt","r") as f1, open('c:\\somefile.txt','w') as f2: 
+0

我嘗試了兩種方法,但都沒有成功。有沒有解決方法? – user61629

+0

這肯定不是問題(或唯一的問題)。當我讀到你的代碼時,它是第一件跳到我身上的東西,它在過去讓我感到困擾。對不起,它並沒有解決你的問題。 – Matthew