Reset the File Pointer to the Beginning in Python

In Python, you can reset the file pointer to the beginning of a file using the seek(0) method. This moves the file pointer to the start, allowing you to read or write from the beginning again. The seek() function is available for file objects opened in read, write, or append modes.


Examples

1. Reset File Pointer using seek(0) in Read Mode

In this example, we open a file in read mode, read some content, reset the pointer, and read again.

main.py

</>
Copy
# Open the file in read mode
with open("sample.txt", "r") as file:
    print("First read:", file.readline())  # Read the first line
    
    # Reset file pointer to the beginning
    file.seek(0)
    
    print("Second read:", file.readline())  # Read the first line again

Explanation:

Here, the file sample.txt is opened in read mode. The first call to readline() reads and prints the first line. Then, we use file.seek(0) to reset the file pointer back to the beginning. The second call to readline() again reads the first line.

Output:

2. Reset File Pointer While Reading the Entire File

We can also reset the file pointer after reading the entire file to read it again.

main.py

</>
Copy
# Open the file in read mode
with open("sample.txt", "r") as file:
    content = file.read()  # Read full content
    print("First Read:\n", content)
    
    # Reset file pointer
    file.seek(0)
    
    content_again = file.read()
    print("Second Read:\n", content_again)

Explanation:

The read() function reads the entire file content. After that, we use seek(0) to reset the pointer and read the entire content again. This is useful when processing a file multiple times without reopening it.

Output:

3. Reset File Pointer in Write Mode

When opening a file in write mode, resetting the file pointer can overwrite data from the beginning.

main.py

</>
Copy
# Open file in write mode
with open("sample.txt", "w") as file:
    file.write("Hello World!")  # Write data
    file.seek(0)
    file.write("Python")  # Overwrite from the beginning

Explanation:

The file is opened in write mode, and “Hello World!” is written. Using seek(0), the pointer moves back to the beginning, and “Python” is written, overwriting the first part.

Output (Content of sample.txt):

4. Reset File Pointer in Append Mode

Even when using append mode (a+), resetting the file pointer allows reading the existing content.

main.py

</>
Copy
# Open file in append mode
with open("sample.txt", "a+") as file:
    file.write("\nNew Line Added")  # Append data
    
    # Reset pointer to beginning for reading
    file.seek(0)
    print("File Content:\n", file.read())

Explanation:

The file is opened in append mode, and a new line is added. Since a+ mode does not allow reading unless reset, we use seek(0) before calling read().

Output:

Conclusion

The seek(0) method in Python is essential for resetting the file pointer, allowing re-reading, overwriting, or accessing content from the beginning. Here’s a summary of when to use it:

  1. In Read Mode: Re-read content from the start.
  2. In Write Mode: Overwrite content from the beginning.
  3. In Append Mode: Read existing content before appending.