-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyte_extractor.py
More file actions
executable file
·51 lines (40 loc) · 1.27 KB
/
Copy pathbyte_extractor.py
File metadata and controls
executable file
·51 lines (40 loc) · 1.27 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
#!/usr/bin/python3
import argparse
import sys
STEP = 1024 # Step for manual seek (when file is not seekable)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--count", "-c", type=int, default=-1,
help="Number of characters to read")
parser.add_argument("--offset", "-s", type=int, default=0, help="Offset")
parser.add_argument("--output", "-o", help="Output file")
parser.add_argument("input", help="Input file; use - for standard input")
args = parser.parse_args()
if args.input == "-":
f = sys.stdin
else:
f = open(args.input)
# Read only the necessary bytes if it is possible
if f.seekable():
f.seek(args.offset)
s = f.read(args.count)
# Some files like sys.stdin are not seekable... Let's seek manually.
else:
n = 0
while (n + STEP) < args.offset:
# Read some bytes
f.read(STEP)
n += STEP
# Read the remaining bytes to complete offset
f.read(args.offset - n)
# Read the wanted bytes
s = f.read(args.count)
if args.input != "-":
f.close()
if args.output:
f = open(args.output, "w")
f.write(s)
f.close()
else:
print(s)