2015-10-08 36 views
1

似乎在for循環中有語法錯誤,但我不知道爲什麼。我對Python有些新鮮,我在Centos上嘗試了這一點。Python for循環語法錯誤

fil = open('acnt_data_text.txt', 'r') 
for data_str in fil: 
    result = subprocess.call(["php", "class.php"], data_str) 

print result 

fil.close() 

語法錯誤:invalid syntax: for data_str in fil:

+1

在你的代碼確保間隔開這條線'結果=子.call([「php」,「class.php」],data_str)'在您的for循環中。 – idjaw

回答

1

你應該有另一條線路上冒號(result = subprocess.call etc...)之後的東西,所以它更容易閱讀。此外,這可能會修復語法錯誤。

2

我想應該是這樣

fil = open('acnt_data_text.txt', 'r') 
for data_str in fil: 
    result = subprocess.call(["php", "class.php"], data_str) 
print result 

fil.close() 

可避免關閉文件,如果使用with,而不是像這樣

with open('acnt_data_text.txt', 'r') as fil: 
    for data_str in fil: 
     result = subprocess.call(["php", "class.php"], data_str) 
    print result 
+0

工作,謝謝你的幫助。 – rancper