Sometimes, we want to tail a log file in Python.
In this article, we’ll look at how to tail a log file in Python.
How to tail a log file in Python?
To tail a log file in Python, we can run tail
with the sh
module.
For instance, we write:
from sh import tail
for line in tail("-f", "foo.txt", _iter=True):
print(line)
We call tail
with the file name and _iter
set to True
to let us run an infinite loop and print out line
which has the output.
Conclusion
To tail a log file in Python, we can run tail
with the sh
module.