Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does, With open() not works with python 2.6

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)
like image 632
Sara Avatar asked Aug 27 '12 08:08

Sara


People also ask

What is not the difference between Python 2 and Python 3?

Python 3 has an easier syntax compared to Python 2. A lot of libraries of Python 2 are not forward compatible. A lot of libraries are created in Python 3 to be strictly used with Python 3. Python 2 is no longer in use since 2020.

How do I run Python 3 code in Python 2?

To use the Python 3 processor for Python code within a program block, use BEGIN PROGRAM PYTHON3-END PROGRAM . By default, Python scripts that are run from the SCRIPT command are run with the Python 2 processor. To run a script that uses the Python 3 processor, use PYTHONVERSION=3 on the SCRIPT command.

How do I know if a script is Python 2 or 3?

If you want to determine whether Python2 or Python3 is running, you can check the major version with this sys. version_info. major . 2 means Python2, and 3 means Python3.

Can you nest with open?

It's also possible to nest with open statements instead of using two open statements in the same line.


2 Answers

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.
like image 156
Martijn Pieters Avatar answered Sep 20 '22 00:09

Martijn Pieters


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.

like image 33
glglgl Avatar answered Sep 21 '22 00:09

glglgl