7

I am trying to use "With open()" with python 2.6 and it is giving error(Syntax error) while it works fine with python 2.7.3 Am I missing something or some import to make my program work!

Any help would be appreciated.

Br

My code is here:

def compare_some_text_of_a_file(self, exportfileTransferFolder, exportfileCheckFilesFolder) :
    flag = 0
    error = ""
    with open("check_files/"+exportfileCheckFilesFolder+".txt") as f1,open("transfer-out/"+exportfileTransferFolder) as f2:

        if f1.read().strip() in f2.read():
            print ""
        else:
            flag = 1
            error = exportfileCheckFilesFolder
            error = "Data of file " + error + " do not match with exported data\n"
        if flag == 1:   
            raise AssertionError(error)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Sara
  • 585
  • 7
  • 12
  • 22
  • 2
    if you have the literal line `with open()` this will give a syntax error even in 2.7. Can you update your question with the code that is giving the syntax error? – Burhan Khalid Aug 27 '12 at 09:19

3 Answers3

11

The with open() statement is supported in Python 2.6, you must have a different error.

See PEP 343 and the python File Objects documentation for the details.

Quick demo:

Python 2.6.8 (unknown, Apr 19 2012, 01:24:00) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('/tmp/test/a.txt') as f:
...     print f.readline()
... 
foo

>>> 

You are trying to use the with statement with multiple context managers though, which was only added in Python 2.7:

Changed in version 2.7: Support for multiple context expressions.

Use nested statements instead in 2.6:

with open("check_files/"+exportfileCheckFilesFolder+".txt") as f1:
    with open("transfer-out/"+exportfileTransferFolder) as f2:
        # f1 and f2 are now both open.
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • @Martjin why did I get a ban ! can you help me with that please ! – Sara Aug 27 '12 at 09:37
  • @Sara: See [What can I do when getting "Sorry, we are no longer accepting questions/answers from this account"?](http://meta.stackexchange.com/q/86997) – Martijn Pieters Aug 27 '12 at 09:40
  • @Martjin I have alreay read all that, and I could not find any solution for it ! – Sara Aug 27 '12 at 09:46
  • @Sara: That's all I can do for you; the process is fully automated, noone can help you along there I'm afraid. – Martijn Pieters Aug 27 '12 at 09:47
  • @Martjin please if you like my post(I mean give a positive rating it may help). – Sara Aug 27 '12 at 09:50
  • 1
    @Sara: Including more detail would help much more. It is not just this question that would have triggered the ban, it is based on your overall use of Stack Overflow, not on just one post. Adding more detail like your exact code, the stack trace of the error, etc. would have helped me give you much more information about what goes wrong, for example. – Martijn Pieters Aug 27 '12 at 09:53
  • @Martjin The exact code can be found in separate post where I have pasted the code :) – Sara Aug 27 '12 at 09:57
  • 1
    @Sara: I more meant you should probably have posted it in this question in the first place; the lack of such detail is probably why it was voted down to start with. – Martijn Pieters Aug 27 '12 at 09:58
  • ok thanks for all the help :) lets keeping my fingers cross, admin would understand my situation and release my ban ! – Sara Aug 27 '12 at 10:02
  • @Sara If you do the mentionned changes (adding relevant code to your question and removing the answer containing it) might speed up the process. – glglgl Aug 27 '12 at 11:27
  • 1
    @glglgl I say thanks and I would take care of all this stuff in future :) – Sara Aug 28 '12 at 07:24
5

It is the "extended" with statement with multiple context expressions which causes your trouble.

In 2.6, instead of

with open(...) as f1, open(...) as f2:
    do_stuff()

you should add a nesting level and write

with open(...) as f1:
    with open(...) as f2:
        do.stuff()

The docu says

Changed in version 2.7: Support for multiple context expressions.

glglgl
  • 89,107
  • 13
  • 149
  • 217
0

The with open() syntax is supported by Python 2.6. On Python 2.4 it is not supported and gives a syntax error. If you need to support PYthon 2.4, I would suggest something like:

def readfile(filename, mode='r'):
    f = open(filename, mode)
    try:
        for line in f:
            yield f
    except e:
        f.close()
        raise e
    f.close()

for line in readfile(myfile):
    print line
reece
  • 7,945
  • 1
  • 26
  • 28
  • why did I get a ban ! can you help me with that pleash ! – Sara Aug 27 '12 at 09:37
  • can you give a positive ranking to my qustion please:) it would help me lifting my ban ! please – Sara Aug 27 '12 at 11:20
  • @Sara This is not helpful as well: adding these requests to a completely unrelated answer. If, then put them to your question. – glglgl Aug 27 '12 at 11:23
  • and concerning "can you give a positive ranking to my qustion please": people will do that if they think it is a good question. Now that it contains the code, it might be considered good and people might start upvoting. – glglgl Aug 27 '12 at 11:41