-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtail.py
More file actions
88 lines (77 loc) · 3.03 KB
/
tail.py
File metadata and controls
88 lines (77 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
'''
Name: Hamdy Abou El Anein
Email: hamdy.aea@protonmail.com
Date of creation: 5-11-2024
Last update: 5-11-2024
Version: 1.0
Description: The tail command from GNU coreutils in python3.
Example of use: python3 tail.py -n 20 myfile.txt
'''
import os
import sys
import time
import argparse
def tail(file, num_lines=10, num_bytes=None, follow=False, zero_terminated=False):
if file == '-':
f = sys.stdin
else:
f = open(file, 'rb' if num_bytes is not None else 'r')
if num_bytes is not None:
# Read the last num_bytes
f.seek(0, os.SEEK_END)
size = f.tell()
start = max(0, size - num_bytes)
f.seek(start)
data = f.read()
if zero_terminated:
print(data.decode(errors='replace').replace('\n', '\0'), end='')
else:
print(data.decode(errors='replace'), end='')
else:
# Read the last num_lines
lines = []
if zero_terminated:
for line in f:
lines.append(line.replace(b'\0', b'\n').decode(errors='replace'))
if len(lines) > num_lines:
lines.pop(0)
else:
for line in f:
lines.append(line) # No decode needed here
if len(lines) > num_lines:
lines.pop(0)
print(''.join(lines), end='')
if follow:
try:
while True:
if not os.path.exists(file): # Check if the file still exists
print(f"\nFile '{file}' has been removed or renamed.")
break
line = f.readline()
if not line:
time.sleep(1)
continue
if zero_terminated:
print(line.replace(b'\0', b'\n').decode(errors='replace'), end=' ')
else:
print(line, end='') # No decode needed here either
except KeyboardInterrupt:
print("\nStopped by user.")
finally:
f.close() # Ensure the file is closed properly
def main():
parser = argparse.ArgumentParser(description='Output the last part of files')
parser.add_argument('files', nargs='*', default=['-'], help='Files to read from')
parser.add_argument('-n', '--lines', type=int, default=10, help='Output the last NUM lines')
parser.add_argument('-c', '--bytes', type=int, help='Output the last NUM bytes')
parser.add_argument('-f', '--follow', action='store_true', help='Output appended data as the file grows')
parser.add_argument('-z', '--zero-terminated', action='store_true', help='Line delimiter is NUL, not newline')
args = parser.parse_args()
for file in args.files:
if args.bytes is not None and args.lines is not None:
print("Error: Cannot use both -c and -n options.")
sys.exit(1)
tail(file, num_lines=args.lines, num_bytes=args.bytes, follow=args.follow, zero_terminated=args.zero_terminated)
if __name__ == '__main__':
main()