Open a File for Both Reading and Writing in Python

In Python, you can open a file for both reading and writing using the built-in open() function with mode "r+" (read and write) or "w+" (write and read). These modes allow you to read data from and write data to the same file.


Examples for Opening a File for Both Reading and Writing

1. Using r+ Mode (Reading and Writing Without Overwriting)

The "r+" mode allows us to read from and write to the file. It does not truncate the file, meaning existing content is preserved.

In this example, we will consider a file example.txt, open it in read mode, and print its content to output.

main.py

</>
Copy
# Open the file in "r+" mode
with open("example.txt", "r+") as file:
    # Read the original content
    content = file.read()
    print("Original Content:", content)

    # Move the cursor to the end and append new data
    file.write("\nNew line added!")

# Reopen the file to confirm the changes
with open("example.txt", "r") as file:
    updated_content = file.read()
    print("Updated Content:", updated_content)

Explanation:

Here, we use open("example.txt", "r+") to open the file in read and write mode.

  1. file.read(): Reads the existing content of the file.
  2. file.write("\nNew line added!"): Writes new data to the file without erasing the existing content.
  3. We then reopen the file in "r" mode to verify the updates.

Output:

2. Using w+ Mode (Overwriting and Reading)

The "w+" mode opens the file for both reading and writing but truncates (clears) the file before writing new content.

main.py

</>
Copy
# Open the file in "w+" mode
with open("example.txt", "w+") as file:
    # Writing new content (this will erase previous data)
    file.write("This is new content.")

    # Move the cursor back to the beginning
    file.seek(0)

    # Read and print the updated content
    updated_content = file.read()
    print("Updated Content:", updated_content)

Explanation:

  1. open("example.txt", "w+"): Opens the file for both reading and writing but clears existing content.
  2. file.write("This is new content."): Writes new content to the file.
  3. file.seek(0): Moves the file pointer back to the beginning.
  4. file.read(): Reads and displays the newly written content.

Output:

3. Using a+ Mode (Appending and Reading)

The "a+" mode allows reading from and appending new content to the file. It does not overwrite existing content.

main.py

</>
Copy
# Open the file in "a+" mode
with open("example.txt", "a+") as file:
    # Append new content
    file.write("\nAnother new line.")

    # Move the cursor back to the beginning
    file.seek(0)

    # Read and print the updated content
    updated_content = file.read()
    print("Updated Content:", updated_content)

Explanation:

  1. open("example.txt", "a+"): Opens the file for reading and appending.
  2. file.write("\nAnother new line."): Adds new content without deleting existing data.
  3. file.seek(0): Moves the pointer to the start of the file.
  4. file.read(): Reads the full content of the file, including the newly appended line.

Output:

Conclusion

In this tutorial, we have covered different modes to open a file for both reading and writing:

  1. "r+": Reads and writes without truncating the file.
  2. "w+": Reads and writes but clears the file before writing.
  3. "a+": Reads and appends new data without overwriting.