I am looking to do the following:
- Iterate through a file
- assert that a specific condition
is not
in each line - if it returns false, log the offending row
- Continue through the entire file
This is what I currently have:
import logging
with open('checkpoints-results.log') as file:
for row in file:
assert '"status":"Unresolved"' not in row, logging.warning(row)
Right now it's going through and grabbing the first occurrence, but then it stops.
Am I missing something for continuing until I reach the end of the file?
CodePudding user response:
assert
will raise an exception (AssertionError
) if the condition is not met. If an exception is raised, it halts the current control flow and raises until it's caught (or until the program terminates). Typically you use an assert
to indicate something that has gone unexpectedly wrong such that your program should immediately terminate.
What I think you want to do is use a simple if
statement:
import logging
with open('checkpoints-results.log') as file:
for row in file:
if '"status":"Unresolved"' in row:
logging.warning(row)
continue
# do other stuff with the row?