Spaces:
Sleeping
Sleeping
File size: 1,169 Bytes
bca5172 | 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 | #!/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)
|