3
file=open("data.txt", "w+")
txt="James\n"
file.writelines(txt)
file.seek(0,0)
s='Bond\n007'
file.write(s)
myList=[]
for line in file:
     myList.append(line)
print(myList)
file.close()

What I expected is that the file will write as:

James
Bond
007

and that is what actually the file contains when I check it after the execution of file but the problem is with the reading part i.e the output of program:

Expectation

['James\n','Bond\n', '007']

Reality

['James\n']

So Why am I getting this kind of output ?

============

Edited Part

============

Following is what I have observed and according to me can explain this unexpected output. What I observed that the seek() function automatically clears the internal buffer. Here's a verification to this fact:

file=open("d.txt", "w+")
txt="James\n"
file.writelines(txt)
file.seek(0)
file.write("yo")
while True:
    print("This is a test.")

In the above program I had not used flush(), and I executed an infinite loop at the end so that execution won't end so internal buffer is not cleared by python. What happens when I open the file created by this program while the execution is still going on, well I expected that the file will be empty as everything would still be in the internal buffer but that's not true. What I found was that the file contained the text 'James' while the text 'yo' is still in the internal buffer as the program is still running. This proves my point and I tried several other programs also which gave the similar result. One more user had experienced this and asked about it here but unfortunately his question was irresponsibly closed as a duplicate by a user with high reputation (that's common here for users with low reputation.)

Using the above fact we can explain the above output as follows:

First the open function creates a file handle which then creates a file named 'data.txt' in the same directory, then the writelines function stores the string "James\n" in the internal buffer. The seek function then clears the internal buffer and writes 'James' in the file [Note: Now the file pointer is in the beginning]. Now the for loop starts and stores the only line 'James\n' to the mylist variable which is then displayed as output.

Tushar
  • 167
  • 7

3 Answers3

2
file=open("data.txt", "w+")
txt="James\n"
file.writelines(txt)
# file.seek(0,0)
s='Bond\n007'
file.write(s)
file.seek(0,0)
myList=[]
for line in file:
     myList.append(line)
print(myList)
file.close()

explain: place file.seek(0, 0) before you read the output.

  • but why did it gave ['James\n'] as the output at first ? shouldn't it overwrite it with the other value ? – Arsenic Sep 28 '20 at 04:20
1

It might be because you're open method is in write mode. When you read back the file it gives the desired result.

file=open("data.txt", "r")
txt = file.read()
txt.split('\n')

#['James', 'Bond', '007']
willwrighteng
  • 1,411
  • 11
  • 25
0

With the method seek you're setting the pointer at the beginning.

If your invert the lines and use seek after writing the lines it solves the problem.

file=open("data.txt", "w+")
txt="James\n"
file.writelines(txt)
s='Bond\n007'
file.write(s)
file.seek(0,0)
myList=[]
for line in file:
     myList.append(line)
print(myList)
file.close()

The strange thing is that you set the cursor to the beginning after writing James . So in your example, James should come after Bond and 007. But I didn't manage to find why is not the output.

AlexisG
  • 2,476
  • 3
  • 11
  • 25