Python Forum
seek method clarification
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
seek method clarification
#1
Hi there,
I would like someone to clarify the seek method. I have this code (excuse formatting)

in_file = open('mydata4.txt','r+')
print(in_file.readline())
in_file.seek(0)
in_file.write('Hi!')
in_file.seek(0)
print(in_file.readline())
in_file.close()

which outputs

First Line
Hi!st Line

and I understand what is happening but when I do this

in_file = open('mydata4.txt','r+')
print(in_file.readline())
in_file.seek(0)
in_file.write('Hi!')
#in_file.seek(0) <-- comment out this line
print(in_file.readline())
in_file.close()

the output is

First LineHi!

and I just don't see why 'Hi!' doesn't overwrite the text after the first seek(0) and why is the second seek(0) even needed?

Thanks
Pete
Reply


Messages In This Thread
seek method clarification - by petepiro - Jan-17-2018, 01:23 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020