0

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.

4
  • It could also be misplaced indentation or you forgot to use a tab instead of spaces. Commented Sep 14, 2012 at 16:08
  • 2
    Can you give a few more lines of context? Commented Sep 14, 2012 at 16:09
  • @squiguy: that usually leads to an IndentationError instead. Commented Sep 14, 2012 at 16:10
  • 3
    Which version of Python are you using? I'd guess 2.5.6 or earlier; I can match the error message (even down to the carat pointing at the "n" in open). Commented Sep 14, 2012 at 16:35

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This is the line (see edit above) context with the line before... I've looked up and down the script, i've not used tabs on double-spaces and checked the barckets and parenthesis as well...
Test for indentation with python -tt scriptname.py. Nothing obvious from that line, could be before that still.
it should be two-space indents
@MHibbin: Then it's not indentation that's the problem (though 2-space indents are in my opinion unreadable).
0

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.

2 Comments

I think that might be it... There are two instances of python on the system (one is possibly 2.4, but def not 2.7). and the instance I normally use comes packaged with py2.7. I'll test this back at work
Yes this was the issue! Thanks very much. I will have to remember this when testing in future

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.