This is the python script sum_columns.py:
#!/usr/bin/env python2.7 import sys num_nodes = 0 num_edges = 0 for line in sys.stdin: line = line.strip() col_1, col_2 = line.split(',', 1) col_1 = int(col_1) col_2 = int(col_2) num_nodes += col_1 num_edges += col_2 print '%s, %s' % (num_nodes, num_edges) There's a file called outputs.dat with 1 line:
10, 10 I am trying to redirect the output of the python script into the input file outputs.dat, but when I do this, it writes out 0, 0, when I am expecting 10, 10. When I redirect to another file not named outputs.dat I get 10, 10. Why is this happening?
Here are the commands I am executing.
[local_serv]$ cat outputs.dat 10, 10 [local_serv]$ cat outputs.dat | ./sum_columns.py 10, 10 [local_serv]$ cat outputs.dat | ./sum_columns.py > test.dat; cat test.dat 10, 10 [local_serv]$ cat outputs.dat | ./sum_columns.py > outputs.dat; cat outputs.dat 0, 0 [local_serv]$ My understanding is that > rewrites outputs.dat (if it already exists), why would it rewrite it as 0, 0 when the python script prints 10, 10?