我關於Python 2.4和2.7都嘗試它似乎是在2.4發生同樣的錯誤,並且不會在2.7
Python 2.4中 - 我沒有得到你得到了確切的同樣的錯誤。
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
據我所知,你想用它不支持Python 2.4中打開即可使用。
它也可能是錯位的縮進,或者您忘記使用製表符而不是空格。 – squiguy
你可以提供更多的上下文? – pR0Ps
@squiguy:那*通常會導致一個'IndentationError'。 –