Reputation: 1185
I've used a statement like the following before, however when I try using something similar it returns an error....
File "test.py", line 73
with open(hostsTxt, 'a+') as f1, open(hostsCSV,'a+') as f2, open(hostNameLook, 'a+') as f3, open(webHostsTxt,'a+') as f4:
^
SyntaxError: invalid syntax
Syntax with line above:
if hostName != "*" and hostIP != "*":
with open(hostsTxt, 'a+') as f1, open(hostsCSV,'a+') as f2, open(hostNameLook, 'a+') as f3, open(webHostsTxt,'a+') as f4:
Any thoughts would be welcomed.
Upvotes: 0
Views: 741
Reputation: 1778
I tried both on Python 2.4 and 2.7 and it seems that same error happens on 2.4 and does not on 2.7
Python 2.4 - I did get the exact same error that you got.
Python 2.4.3 (#1, Nov 3 2010, 12:52:40)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> if hostName != "*" and hostIP != "*":
... with open(hostsTxt, 'a+') as f1, open(hostsCSV,'a+') as f2, open(hostNameLook, 'a+') as f3, open(webHostsTxt,'a+') as f4:
File "<stdin>", line 2
with open(hostsTxt, 'a+') as f1, open(hostsCSV,'a+') as f2, open(hostNameLook, 'a+') as f3, open(webHostsTxt,'a+') as f4:
^
SyntaxError: invalid syntax
Python 2.7
Launching python -O
Python 2.7.2 (default, Apr 17 2012, 22:01:25)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> hostIP ='localhost'
>>> hostName = 'abcd'
>>> if hostName != "*" and hostIP != "*":
... with open(hostsTxt, 'a+') as f1, open(hostsCSV,'a+') as f2, open(hostNameLook, 'a+') as f3, open(webHostsTxt,'a+') as f4:
... print 'testing'
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name 'hostsTxt' is not defined
As far as I know, you are trying to use with open with python 2.4 which is not supported.
Upvotes: 0
Reputation: 1121486
Look at the lines before it, there will be a parenthesis or bracket missing.
That, or you have a python version that doesn't support with
at all, the syntax wasn't introduced until python 2.6.
Upvotes: 7