Using Multiple File Handles for Single File
Posted
by Ryan Rosario
on Stack Overflow
See other posts from Stack Overflow
or by Ryan Rosario
Published on 2010-05-07T05:12:16Z
Indexed on
2010/05/07
5:18 UTC
Read the original article
Hit count: 383
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.
© Stack Overflow or respective owner