Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error when using "with open" in Python (python newbie)

[root@234571-app2 git]# ./test.py 
  File "./test.py", line 4
    with open("/home/git/post-receive-email.log",'a') as log_file:
            ^
SyntaxError: invalid syntax

The code looks like this:

[root@234571-app2 git]# more test.py 
#!/usr/bin/python
from __future__ import with_statement

with open("/home/git/post-receive-email.log",'a') as log_file:
    log_file.write("hello world")

and I am using Python 2.5.5

[root@234571-app2 git]# python -V
Python 2.5.5
like image 751
Tony Avatar asked Apr 21 '10 17:04

Tony


People also ask

How do I fix invalid syntax error in Python?

You can clear up this invalid syntax in Python by switching out the semicolon for a colon. Here, once again, the error message is very helpful in telling you exactly what is wrong with the line.

What is with open () in Python?

The with statement works with the open() function to open a file. Unlike open() where you have to close the file with the close() method, the with statement closes the file for you without you telling it to.

Why is Python giving me syntax error?

The Python SyntaxError occurs when the interpreter encounters invalid syntax in code. When Python code is executed, the interpreter parses it to convert it into bytecode. If the interpreter finds any invalid syntax during the parsing stage, a SyntaxError is thrown.

What is syntax error in Python with example?

Syntax errors are mistakes in the use of the Python language, and are analogous to spelling or grammar mistakes in a language like English: for example, the sentence Would you some tea? does not make sense – it is missing a verb. Common Python syntax errors include: leaving out a keyword.


2 Answers

What you have should be correct. Python 2.5 introduced the with statement as something you can import from __future__. Since your code is correct, the only explanation I can think of is that your python version is not what you think it is. There's a good chance you have multiple versions of python installed on the system and for some reason your code is running with an older version. Try running it like this:

[root@234571-app2 git]# /usr/bin/python2.5 test.py

Assuming this works, you can change your first line to indicate which version of python you'd like. That can either be a direct path to python2.5 or you can use the env command to search the user's PATH variable for python2.5. The correct approach depends on what your systems python installs are. Here are the 2 approaches:

To use /usr/bin/python2.5 directly, you can do this:

#!/usr/bin/python2.5

To use whichever version of python2.5 occurs first in your PATH, do this:

#!/usr/bin/env python2.5
like image 85
Benson Avatar answered Sep 20 '22 06:09

Benson


Maybe like this?

#!/usr/bin/env python2.5
from __future__ import with_statement

with open("/home/git/post-receive-email.log",'a') as log_file:
    log_file.write("hello world")
like image 23
Krumelur Avatar answered Sep 21 '22 06:09

Krumelur