python - SyntaxError using `with open...` -
there several questions on seem deal problem. however, seem caused using pre-2.6 version of python. not case here. appreciate tracking down cause of this.
i syntax error when try use with open() f
construct.
here's code (test.py):
#!/usr/bin/env python import sys print sys.version fname = "/tmp/test.txt" open(fname, 'a').close() open(fname, 'a') fh fh.write("test")
here's output:
$ ./test.py file "./test.py", line 10 open(fname, 'a') fh ^ syntaxerror: invalid syntax
python version being used:
$ /usr/bin/env python python 2.7.10 (default, oct 14 2015, 16:09:02) [gcc 5.2.1 20151010] on linux2 type "help", "copyright", "credits" or "license" more information. >>> $ whereis python python: /usr/bin/python3.4 /usr/bin/python /usr/bin/python3.4m /usr/bin/python3.3 /usr/bin/python3.3m /usr/bin/python2.7 /usr/lib/python3.4 /usr/lib/python3.3 /usr/lib/python3.5 /usr/lib/python2.7 /etc/python3.4 /etc/python /etc/python3.3 /etc/python2.7 /usr/local/lib/python3.4 /usr/local/lib/python3.3 /usr/local/lib/python2.7 /usr/include/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz
my system info:
$ uname -a linux ... 4.2.0-27-generic #32-ubuntu smp fri jan 22 04:49:08 utc 2016 x86_64 x86_64 x86_64 gnu/linux
thanks!
you need :
after with
statement
with open(fname, 'a') fh: fh.write("test")
Comments
Post a Comment