2012-09-14 30 views
0

我用如下語句之前以下,但是當我嘗試使用類似它返回一個錯誤的東西....Python語法錯誤...不知道爲什麼

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 

語法上面一行:

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: 

任何想法都會受到歡迎。

+0

它也可能是錯位的縮進,或者您忘記使用製表符而不是空格。 – squiguy

+2

你可以提供更多的上下文? – pR0Ps

+0

@squiguy:那*通常會導致一個'IndentationError'。 –

回答

0

我關於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中打開即可使用。

+0

我認爲這可能是......系統上有兩個python實例(一個可能是2.4,但不是2.7)。而我通常使用的實例包裝在py2.7中。我會在工作中測試這個 – MHibbin

+0

是的,這是問題!非常感謝。將來測試時我將不得不記住這一點 – MHibbin

7

之前的行吧,會有一個括號或括號缺失。

那麼,或者你有一個根本不支持with的python版本,直到python 2.6才引入該語法。

+0

這是行前(上面的編輯)上下文與行之前...我已經上下查看腳本,我沒有使用雙空格的選項卡,並檢查了括號和括號以及... – MHibbin

+0

用'python -tt scriptname.py'測試縮進。這條線沒有任何明顯的表現,可能在此之前。 –

+0

真的,1格縮進? –

相關問題