-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_file_process.py
More file actions
63 lines (53 loc) · 2.26 KB
/
log_file_process.py
File metadata and controls
63 lines (53 loc) · 2.26 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
import sys
from pathlib import Path
def remove_2025_lines(input_path, output_path=None):
"""
Remove all lines starting with '2025' or that are blank from a single text/log file.
:param input_path: Path to the input file (e.g. 'app.log').
:param output_path: Path to save the filtered file. If None, overwrites input_path.
"""
input_path = Path(input_path)
if output_path is None:
output_path = input_path
else:
output_path = Path(output_path)
# Read & filter
with input_path.open('r', encoding='utf-8') as f:
filtered = [
line
for line in f
# drop lines that start with '2025'
if not line.startswith('2025')
# drop lines that are blank or only whitespace
and line.strip() != ''
]
# Write back
with output_path.open('w', encoding='utf-8') as f:
f.writelines(filtered)
def batch_remove_2025_from_logs(directory, in_place=True):
"""
Remove lines starting with '2025' or blank lines from every .log file in a directory.
:param directory: Path to search for '*.log' files.
:param in_place: If True, overwrite each .log file.
If False, write to a sibling file named '<original>.filtered.log'.
"""
directory = Path(directory)
for log_file in directory.glob('*.log'):
if in_place:
remove_2025_lines(log_file)
else:
filtered_path = log_file.with_name(log_file.stem + '.filtered.log')
remove_2025_lines(log_file, filtered_path)
if __name__ == "__main__":
import argparse
p = argparse.ArgumentParser(description="Strip '2025' and blank lines from log files")
p.add_argument('path', help="File or directory to process")
p.add_argument('--batch', action='store_true',
help="Treat 'path' as a directory and process all .log files in it")
p.add_argument('--no-inplace', action='store_true',
help="When batching, write to '*.filtered.log' instead of overwriting")
args = p.parse_args()
if args.batch:
batch_remove_2025_from_logs(args.path, in_place=not args.no_inplace)
else:
remove_2025_lines(args.path)