I just start learn python, and the code has some error, can’t run. so can you help me address it. it shows
File "ex2.py", line 21
if isRobotRecord(line)
^
SyntaxError: invalid syntax
ex2.py
import time
robot_emails = ["googlebot@google.com"]
robot_emails.append("66.249.74.228")
robot_emails.append("61.147.110.22")
robot_emails.append("61.147.110.21")
robot_emails.append("61.147.112.231")
f = open("/opt/CLiMB/Storage1/log/vsftp.log")
def isRobotRecord(line):
for email in robot_emails:
if email in line.split("Client")[1]:
return False
return True
def OnlyRecent(line):
if time.strptime(line.split("[")[0].strip(),"%a %b %d %H:%M:%S %Y")> time.gmtime(time.time()-(60*60*24*7)):
return True
return False
filename= time.strftime('%Y%m%d')+'.log'
f1= open(filename,'w')
for line in f:
if OnlyRecent(line):
if isRobotRecord(line)
print line
f1.write(line)
f.close()
f1.close()
At a minimum, you need a
:at the end of thatifstatement. That’s what’s causing your error, similar to the following transcript:You may also want to look into your indentation – it looks like that
ifstatement may be indented one too many times. There are eight spaces difference between it and the previous line despite the fact you appear to be using four most everywhere else.In a language where indentation is so important, you have to be particularly careful.