Skip to content

How to read and write to the same file in Python

How to read and write to the same file in Python

In this tutorial we will learn how to read and write to the same file in Python . Before reading further we recommend to read about these tutorials –

Here we will use the file “hello1.txt” and we will keep the file in the current folder where we are working now .

Here is the content of hello1.txt –

Python was invented in 1991 by Guido van Rossum1993Python

How to read and write to the same file in Python using r+ mode

If we open the .txt file in this mode, we can read and write to the same file in Python . When opening the file-pointer is in 0th position. With the help of this mode we can read from any part of the file and write anywhere. The only thing to note is whether the file exists before running the program. If the file does not exist then the program will raise error.

Example 1:

with open("hello1.txt","r+") as fp:
    # this will overwrite the existing content
    fp.write("55")
    # Reading will start after 55
    print(fp.read())
    print("After reading the file position of file pointer is =",fp.tell())
    
    fp.seek(fp.tell()-34,0)
    # filepointer is now at start of 1991
    print("After taking the file pointer 34 letters back position is =",fp.tell())
    
    # it will overwrite the existing content
    fp.write("2")
    # taking filepointer at the end    
    fp.seek(0,2)
    print("After taking the file pointer at the end position is =",fp.tell())
    
    fp.write(" programming language")
    print("After writing the position of file pointer is =",fp.tell())
thon was invented in 1991 by Guido van Rossum1993Python
After reading the file position of file pointer is = 57
After taking the file pointer 34 letters back position is = 23
After taking the file pointer at the end position is = 57
After writing the position of file pointer is = 78
  • In the above code as a result of writing "55" , the first word of the file has changed from "Python" to "55thon" and the file-pointer has moved to "t" of "55thon". Now after reading the file, it has been printed from "t" of "55thon" to the end.
  • Now file-pointer is in the last position for which fp.tell() has returned 57.
  • After that file-pointer has been brought back to 34 letters. Now the file-pointer is in the 23rd position which is the position of "1" in "1991". After that we have written "2" . As a result "1" of "1991" is changed to "2" so "1991" has become "2991" .
  • Then the file-pointer is sent to the very end for which fp.tell()has returned 57 .
  • Then we have written " programming language" which has been added at the end of the previous writing. After that fp.tell() has returned 78 .
This image has an empty alt attribute; its file name is 10.png

How to read and write to the same file in Python using w+ mode

If we open the .txt file in this mode, we also can read and write to the same file in Python . If the file exists then it deletes the content of the file and rewrite it, otherwise it creates a new file. File-pointer is in 0th position at the time of opening. With this mode we can read from any part of the file and write anywhere.

Example 1:

with open("hello1.txt","w+") as fp:
    #all content is erased as it is opened in w+ mode
    print(fp.read())  #it will show nothing
    
    fp.write("Python is the most popular programming language")
    print("After writing position of the file pointer is =",fp.tell())
    
    #this line can not read anything as filepointer is already at the end
    print(fp.read())
    
    #taking back filepointer at the start
    fp.seek(0,0)
    print(fp.read())
    
    #taking back filepointer at 20 letter ahead at p of programming
    fp.seek(fp.tell()-20,0)
    fp.write("55")
    print(fp.read())
After writing position of the file pointer is = 47

Python is the most popular programming language
ogramming language
  • In the above code, the old content of the file has been deleted as we open the file in “w+” mode . For this reason fp.read() could not read anything .
  • Then there is something written in the file for which the file-pointer has gone to the very end and fp.tell() returned 47.
  • Now as the file-pointer is at the very end, nothing can be read using fp.read().
  • Then the file-pointer has been brought to the very beginning and the file has been read.
  • Then the file-pointer was moved 20 letters back and placed at "p" of "programming" . Now we have written "55" which changed "programming" to "55ogramming" and placed file-pointer at "o" of "55ogramming" . Then we read the file again which returns "ogramming language".
This image has an empty alt attribute; its file name is 11.png

How to read and write to the same file in Python using a+ mode

If we open the .txt file in this mode, we also can read write to the same file in Python . If the file exists then the file-pointer is in the last position and adds new text at the end of the file, otherwise it creates a new file. In this case we can read any part of the file but we can write only at the end.

Example 1:

with open("hello1.txt","a+") as fp:
    print("After opening position of file pointer is =",fp.tell())
    
    #filepointer is already at the end so it can not read anything
    print(fp.read())
    
    fp.seek(0,0)
    #now filepointer is at the start now it can read
    print(fp.read())
    
    # taking file pointer 20 letters back at first 5 of 55ogramming
    fp.seek(fp.tell()-20,0)
    # trying to change 55 with pr
    fp.write("pr")
    fp.seek(0,0)
    print(fp.read())
After opening position of file pointer is = 47

Python is the most popular 55ogramming language
Python is the most popular 55ogramming languagepr
  • In the above code, since the file is opened in “a +” mode, the file-pointer has reached at the end for which fp.tell() has returned 47.
  • Then an attempt was made to read the file which is not possible.
  • Then the file-pointer is brought to the beginning and the file is read again which returns "Python is the most popular 55ogramming language".
  • Then the file-pointer is placed 20 letters back at first "5" of "55ogramming" and an attempt is made to write "pr" there. So we can change "55ogramming" to "programming" . But as the file is opened in “a +” mode, the text has been added to the end of the file instead of that place . Then the file-pointer is brought to the beginning and the file is read again which returns "Python is the most popular 55ogramming languagepr".
This image has an empty alt attribute; its file name is 12.png

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply