Using Multiple File Handles for Single File
- by Ryan Rosario
I have an O(n^2) operation that requires me to read line i from a file, and then compare line i to every line in the file. This repeats for all i.
I wrote the following code to do this with 2 file handles, but it does not yield the result I am looking for. I imagine this is a simple error on my part.
IN1 = open("myfile.dat","r")
IN2 = open("myfile.dat","r")
for line1 in IN1:
for line2 in IN2:
print line1.strip(), line2.strip()
IN1.close()
IN2.close()
The result:
Hello Hello
Hello World
Hello This
Hello is
Hello an
Hello Example
Hello of
Hello Using
Hello Two
Hello File
Hello Pointers
Hello to
Hello Read
Hello One
Hello File
The output should contain 15^2 lines.