Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
r+ mode in files
#6
You could use fd.seek() to jump to the end of the file:

with open("file.txt", "w") as fd:
    for line in range(1, 5):
        fd.write(f"Welcome to Python line {line}\n")


with open("file.txt", "r+") as fd:
    print("Position after open:", fd.tell())
    fd.seek(0, 2)
    print("Position after seek:", fd.tell())
    fd.write("Hello World\n")


with open("file.txt", "r") as fd:
    print(fd.read())
Docs: https://docs.python.org/3/library/io.htm...OBase.seek
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
r+ mode in files - by grkiran2011 - Feb-14-2023, 02:03 PM
RE: r+ mode in files - by rob101 - Feb-14-2023, 03:24 PM
RE: r+ mode in files - by grkiran2011 - Feb-15-2023, 11:54 PM
RE: r+ mode in files - by deanhystad - Feb-14-2023, 09:03 PM
RE: r+ mode in files - by grkiran2011 - Feb-15-2023, 11:56 PM
RE: r+ mode in files - by DeaD_EyE - Feb-16-2023, 02:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  things that work in terminal mode but not in sublime mode alok 4 2,976 Aug-11-2021, 07:02 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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