strange behavior of readline() with 'r+' mode in file handling
Program 1- F=open(r'c:\temp\test.txt','w') F.write('Hello how are you\n') F.write('What is your future plan') F.close() F=open(r'c:\cpp\test.txt','r+') print("before write",F.tell()) F.write('\nwelcome to my class') #print("after write",F.tell()) str=F.read() print("after readlines",F.tell()) print(str) F.close() ...................... output: before write 0 after readlines 43 at is your future plan ........................ File temp.txt content welcome to my classat is your future plan ............. *😬* *In program 1 output is as expected but in program 2 output is not as expected * *and in program 3 after introducing one print() statement it is behaving like program1 😤* ............................................................................... Program 2 F=open(r'c:\temp\test.txt','w') F.write('Hello how are you\n') F.write('What is your future plan') F.close() F=open(r'c:\temp\test.txt','w')print("before write",F.tell()) F.write('\nwelcome to my class') #print("after write",F.tell()) str=F.readline() print("after readlines",F.tell()) print(str) F.close() ............ output: before write 0 after readlines 40 Hello how are you ........ file content Hello how are you What is your future plan welcome to my class -------- Program 3 F=open(r'c:\temp\test.txt','w') F.write('Hello how are you\n') F.write('What is your future plan') F.close() F=open(r'c:\temp\test.txt','w')print("before write",F.tell()) F.write('\nwelcome to my class') print("after write",F.tell()) str=F.readline() print("after readlines",F.tell()) print(str) F.close() ............ output: before write 0 after write 21 after readlines 43 at is your future plan ........ file content welcome to my classat is your future plan
participants (1)
-
Sanjay Pandey