1

I was trying to learn and experiment with Python today, and I got into trying to open a text file, reading the lines from it, and then writing those lines in a 2nd text file. After that, I tried to load the lines from both files into lists, and print those lists in the console. Here is the experimental code:

f = open('D:\\origFile.txt', 'r')
f2 = open('D:\\copyFile.txt', 'w')

for line in f:
    f2.write(line.rstrip() + ' ')
f2.close()

s=""
for linez in f:
    s += linez
    print (s)

s2=""
f2 = open('D:\\copyFile.txt', 'r+')
reading = f2.readlines()
print (reading)

for linex in f2:
    s2 += linex
    print (linex)

list1=s.split()
list2=s2.split()
print(list1)
print(list2)
f.close()
f2.close()

The code works as expected only when I remove or comment this part:

f2 = open('D:\\copyFile.txt', 'w')

for line in f:
    f2.write(line.rstrip() + ' ')
f2.close()

Although I'm already closing f2 file after writing the contents of f1 to it. So why does it affect the rest of the file operations in the code? And how does it still affect f1? As in, the lists in the end are neither populated by the text from f1 nor f2.

Edit 1: In my question, I already tried to close the second file thinking that it would be the cause of my problem, not the loop itself; while the other question didn't even have a loop to satisfy an answer to my question. Although the answers are somewhat the same, the answer in the other question doesn't show how to use the contents of the file repeatedly without having to reset the iterator using f.seek(0) to be able to use the contents in several loops like in my code above.

terasky
  • 13
  • 4
  • 1
    You do `for line in f` and right after `for linez in f`, but you already read all the lines from f. Using new variable name does not rewind the file. – nsilent22 Feb 18 '16 at 21:30
  • So in Python, loops don't rewind? I have to rewind the pointer to the start of the file myself before trying to loop on it again? – terasky Feb 18 '16 at 21:33
  • 1
    It's not the pointer. Consider it as a stream, from which you "feed" data into your `line` or `linez` variable. If you empty the stream you'll get no more data. Do `f.seek(0)` to rewind to the beginning of the stream. – nsilent22 Feb 18 '16 at 21:35
  • See [`f.seek(0)`](https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects). – pzp Feb 18 '16 at 21:45
  • @nsilent22 Got it. I did guess it's a problem regarding the iterator, but I didn't know how to set it to the beginning of the file using `f.seek(0)`.. and another way to do it is as described below by John Gordon.. Thank you for your help. – terasky Feb 18 '16 at 21:46

1 Answers1

0

You're attempting to loop through the contents of f twice:

for line in f:

for linez in f:

... and you can't do it that way. The second loop will exit immediately, because you already read all of the lines; there are none left to read.

If you want to save the lines of a file and loop through them several times, use something like this:

all_lines = f.readlines()
John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • It's regarding the iterator being at the end of the file by the time I get to the second loop of `for linez in f:`.. Got it, thanks for your help. – terasky Feb 18 '16 at 21:48