简体   繁体   中英

Python for loop syntax error

There seems to be a syntax error at the for loop but I don't know why. I'm somewhat new to Python, and I'm trying this in Centos.

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

print result

fil.close()

SyntaxError: invalid syntax: for data_str in fil:

I think it should be spaced like this

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

fil.close()

You can avoid closing the file if you use with instead like so

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

You should have the stuff after the colon ( result = subprocess.call etc... ) on another line, so it's easier to read. Also, this might fix the syntax error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM