Open a File in Text Mode in Python

In Python, you can open a file in text mode using the open() function with the mode set to 'r' (read), 'w' (write), 'a' (append), or 'x' (exclusive creation). The default mode is text mode ('t'), meaning no special flag is required. Below are various examples demonstrating how to open a file in text mode in Python.


Examples to Open a File in Text Mode

1. Opening a File in Read Mode

The most common way to open a file in text mode is using 'r' (read mode). This allows us to read the contents of a file.

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 text mode for reading
file = open("example.txt", "r")

# Read the content of the file
content = file.read()

# Close the file
file.close()

# Print the content
print(content)

Explanation:

  1. open("example.txt", "r") opens the file example.txt in text mode for reading.
  2. read() reads the entire content of the file.
  3. close() ensures the file is properly closed after reading.

Output:

2. Opening a File in Write Mode

We can open a file in text mode using 'w' (write mode) to write new content. If the file exists, it will be overwritten.

main.py

</>
Copy
# Open the file in text mode for writing
file = open("example.txt", "w")

# Write content to the file
file.write("This is new content written to the file.")

# Close the file
file.close()

Explanation:

  1. open("example.txt", "w") opens the file in write mode, clearing any existing content.
  2. write() writes new content into the file.
  3. close() ensures that the changes are saved properly.

Output:

3. Opening a File in Append Mode

Using 'a' (append mode), we can add new text to an existing file without erasing its content.

main.py

</>
Copy
# Open the file in text mode for appending
file = open("example.txt", "a")

# Append content to the file
file.write("\nThis line is added at the end.")

# Close the file
file.close()

Explanation:

  1. open("example.txt", "a") opens the file in append mode.
  2. write() adds new content without erasing existing data.
  3. close() saves the changes.

Output:

4. Using the with Statement for Automatic File Handling

The with statement is the recommended way to open files, as it automatically closes the file after use.

main.py

</>
Copy
# Open the file using with statement
with open("example.txt", "r") as file:
    content = file.read()

# Print the content
print(content)

Explanation:

  1. with open("example.txt", "r") as file ensures the file is automatically closed after execution.
  2. read() reads the file content.

Output:

Conclusion

In this tutorial, we covered how to open a file in text mode in Python using different modes:

  1. 'r' (Read Mode): Opens the file for reading.
  2. 'w' (Write Mode): Opens the file for writing, replacing existing content.
  3. 'a' (Append Mode): Opens the file for appending without erasing content.
  4. with Statement: Ensures the file is automatically closed after use.