-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_data-M2v1.py
executable file
·51 lines (38 loc) · 1.47 KB
/
process_data-M2v1.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
49
50
51
#!/usr/bin/python
import os
import sys
import time
def main():
# set up globals
#in_filepath = "data/file1.txt"
# check for program name & input file
if len(sys.argv) != 2:
print('Error: incorrect number of arguments')
print()
print('Usage: {} inputfile'.format(sys.argv[0]))
print()
sys.exit(1)
else:
#OK, we have the right #. Grab the input filename
in_filepath = sys.argv[1]
out_filepath = "data/outfile1.txt"
status_count = 1000 # when to give us status updates
write_count = 10 # when to write out running sum
running_sum = 0
# open our output file for continual updates, no buffering
with open(out_filepath, mode='w') as out_fh:
# open input datafile
with open(in_filepath) as in_fh:
for index, line in enumerate(in_fh):
# sum, output, and give status when appropriate
running_sum += int(line)
# if at X line count, write out running sum (flush cache)
if (index + 1) % write_count == 0:
out_fh.write(str(running_sum) + '\n')
out_fh.flush()
# if at X line count, give us the status, and pause
if (index + 1) % status_count == 0:
print("Line {}: {}".format(index + 1, running_sum))
time.sleep(0.25)
if __name__ == '__main__':
main()