-
Notifications
You must be signed in to change notification settings - Fork 54
/
subset.py
48 lines (26 loc) · 957 Bytes
/
subset.py
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
'Save a subset of lines from an input file; start at offset and count n lines'
'default 100 lines starting from 0'
import sys
import argparse
parser = argparse.ArgumentParser( description = "Save a subset of lines from the input file to the output file" )
parser.add_argument( "input_file", help = "path to input file" )
parser.add_argument( "output_file", help = "path to output file" )
parser.add_argument( "-o", "--offset", help = "line number to start from, default 0", type = int, default = 0 )
parser.add_argument( "-l", "--lines", help = "number of lines to write, default 100", type = int, default = 100 )
args = parser.parse_args()
try:
lines = int( sys.argv[4] )
except IndexError:
lines = 100
i = open( args.input_file )
o = open( args.output_file, 'wb' )
offset = args.offset
count = 0
for line in i:
if offset > 0:
offset -= 1
continue
o.write( line )
count += 1
if count >= args.lines:
break