With keyword and jython 2.5.1

I have the following:

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

this gives a syntax error:

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

I am using python netbeans plugin which depends on jython 2.5.1

I added:

 from __future__ import with_statement 

but that didn’t change anything.

Any tips on what to do?

thanks

+4
source share
2 answers

Instructions for several context managers added only in python2.7, see the documentation .

For jython2.5 you will need from __future__ import with_statement to enable the functions of a single context.

Edit:

Interestingly, even jython2.7b2 does not support multiple context managers.

what you can do is nest contexts:

 with open("c:/whatever") as one_file: with open("c:/otherlocation") as other_file: pass # or do things 
+6
source

In your file paths you have "\" in two places, \ x is usually used to indicate hexadecimal characters. Try either using raw strings with "r" or slipping away from your backslashes with another backslash.

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

or

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

Source: https://habr.com/ru/post/1481001/


All Articles