Alessandro Blasetti Alessandro Blasetti - 20 days ago 7
Linux Question

Why does a Python script work on Windows and not in Linux?

I have from one side Windows 7 with Python 2.7.12 and on the other side Red Hat Enterprise Linux Server release 6.5 with Python 2.6.6.

I have a script that works fine on Windows but not on RHEL.

I receive the following syntax error:

with open('pathtofile', 'rb') as f_input, open('pathtofile', 'w') as f_output:
# ^




SyntaxError: invalid syntax


It may be caused by different versions of Python on the two systems?

Answer
with open('pathtofile', 'rb') as f_input, open('pathtofile', 'w') as f_output: 

is not supported by Python 2.6. In that version you can only open one file in the with statement. Instead, you can do

with open('pathtofile', 'rb') as f_input:
    with open('pathtofile', 'w') as f_output: 
Comments