-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
2,345 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import time | ||
import os | ||
|
||
def tail(filename, n=10): | ||
''' | ||
Print the last `n` lines from the file with name `filename` | ||
''' | ||
with open(filename, 'r') as file: | ||
lines = file.readlines() | ||
last_lines = lines[-n:] | ||
for line in last_lines: | ||
print(line, end='') | ||
|
||
def tail_follow(filename): | ||
''' | ||
Continuously monitor the file with name `filename` and | ||
print new lines as they are written to the file. | ||
''' | ||
with open(filename, 'r') as file: | ||
# Move to the end of the file | ||
file.seek(0, os.SEEK_END) | ||
while True: | ||
line = file.readline() | ||
if not line: | ||
time.sleep(0.1) # Sleep briefly | ||
continue | ||
print(line, end='') | ||
|
||
# Example usage: | ||
# tail('my_file.txt') | ||
tail_follow('npyscreen.log') |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.