ThreatIntel / scripts /sample_lines.py
Malek98's picture
Deploy FastAPI backend via automated upload
bca5172 verified
Raw
History Blame Contribute Delete
1.17 kB
#!/usr/bin/env python3
"""Print and save the first N lines of a file (safe for very large files).
Usage: python scripts/sample_lines.py <input-file> <num_lines>
Writes a sample file to prepared_data/samples/<basename>.sample.txt
"""
import sys
from pathlib import Path
def sample_lines(path: Path, n: int = 20):
out_dir = Path('prepared_data') / 'samples'
out_dir.mkdir(parents=True, exist_ok=True)
out_file = out_dir / (path.name + '.sample.txt')
printed = []
with path.open('r', errors='ignore') as fh, out_file.open('w', errors='ignore') as oh:
for i, line in enumerate(fh):
if i >= n:
break
printed.append(line.rstrip('\n'))
oh.write(line)
print(f"Wrote sample {out_file} ({len(printed)} lines)")
print('\n'.join(printed))
if __name__ == '__main__':
if len(sys.argv) < 2:
print('Usage: sample_lines.py <input-file> [num_lines]')
sys.exit(1)
p = Path(sys.argv[1])
n = int(sys.argv[2]) if len(sys.argv) > 2 else 20
if not p.exists():
print('File not found:', p)
sys.exit(2)
sample_lines(p, n)