Lesson 19 of 30
In Progress

Reading and Writing Text Files

Reading and writing text files is a common task in Python, and there are several built-in functions and methods that make it easy to work with text files.

Reading a Text File

To read a text file in Python, you can use the “open” function followed by the “read” method. For example:

with open("filename.txt", "r") as file: 
    contents = file.read()

In the above example, the “open” function opens the file “filename.txt” in read mode, and the “read” method reads the contents of the file into a string. The “with” statement is used to ensure that the file is properly closed after reading.

Writing to a Text File

To write to a text file in Python, you can use the “open” function followed by the “write” method. For example:

with open("filename.txt", "w") as file: 
    file.write("This is some text.")

In the above example, the “open” function opens the file “filename.txt” in write mode, and the “write” method writes the string “This is some text.” to the file. The “with” statement is used to ensure that the file is properly closed after writing.

Appending to a Text File

You can also use the “append” mode to add text to the end of an existing file, rather than overwriting the file’s contents. For example:

with open("filename.txt", "a") as file: 
    file.write("This is some more text.")

In the above example, the “open” function opens the file “filename.txt” in append mode, and the “write” method adds the string “This is some more text.” to the end of the file.

It’s important to note that when you open a file in write or append mode, any existing content in the file will be overwritten or added to, respectively. To avoid losing data, make sure to use read mode when you only want to read from a file, and make a copy of the file before writing or appending to it if you need to keep the original data.

Conclusion

Overall, reading and writing text files in Python is a simple and straightforward task, and the built-in “open” and “read”/”write” methods make it easy to work with text files in your code.

Exercises

Here are some exercises with solutions to help you practice what you just learned:

How do you open a text file in Python for reading?

To open a text file in Python for reading, you can use the “open” function followed by the “read” method. For example:

with open("filename.txt", "r") as file:
    contents = file.read()

In the above example, the “open” function opens the file “filename.txt” in read mode, and the “read” method reads the contents of the file into a string. The “with” statement is used to ensure that the file is properly closed after reading.

How do you open a text file in Python for writing?

To open a text file in Python for writing, you can use the “open” function followed by the “write” method. For example:

with open("filename.txt", "w") as file:
    file.write("This is some text.")

In the above example, the “open” function opens the file “filename.txt” in write mode, and the “write” method writes the string “This is some text.” to the file. The “with” statement is used to ensure that the file is properly closed after writing.

How do you open a text file in Python for appending?

To open a text file in Python for appending, you can use the “open” function followed by the “write” method in append mode. For example:

with open("filename.txt", "a") as file:
    file.write("This is some more text.")

In the above example, the “open” function opens the file “filename.txt” in append mode, and the “write” method adds the string “This is some more text.” to the end of the file.

What is the “with” statement used for when working with text files in Python?

The “with” statement is used when working with text files in Python to ensure that the file is properly closed after reading or writing. When a file is opened with the “with” statement, it is automatically closed at the end of the block, regardless of whether an exception occurs. This helps to prevent resource leaks and ensure that the file is always closed when you are finished working with it.

How do you read the contents of a text file into a list in Python?

To read the contents of a text file into a list in Python, you can use the “open” function followed by the “readlines” method. For example:

with open("filename.txt", "r") as file: 
    contents = file.readlines()

In the above example, the “open” function opens the file “filename.txt” in read mode, and the “readlines” method reads the contents of the file into a list of strings, with each line of the file being a separate element in the list. The “with” statement is used to ensure that the file is properly closed after reading.