Reading and Writing Text Files



Reading and Writing Text Files in Python

Python Programming: Reading and Writing Text Files

Python offers several ways to read and write text files. Text files are files that contain readable characters instead of binary data. Below are some ways to read and write text files in Python.

Reading Text Files

The basic function for reading lines from a text file in Python is open(). It takes two arguments: the filename to be opened and the mode in which the file should be opened (read, write, append, etc).

file = open("filename.txt", "r")

The mode for reading a file is "r". If the file doesn't exist, an error will be thrown. When the file is open, we can use the read() function to read the entire contents at once or readline() to read one line at a time.

# read entire contents of file at once
contents = file.read()

# read one line at a time
line1 = file.readline()
line2 = file.readline()

Example: Reading a Text File

Suppose we have a file called "sample.txt" with the following contents:

This is the first line.
    This is the second line.
    This is the third line.

To read this file and print the contents to the console, we would use the following code:

file = open("sample.txt", "r")
contents = file.read()
print(contents)

Writing Text Files

The basic function for writing to text files in Python is also open(). However, we need to change the mode to "w" for write. If the file doesn't exist, it will be created. If it exists, the contents will be overwritten.

file = open("filename.txt", "w")

Once the file is open, we can use the write() function to write text to the file. We can also use writelines() to write a list of strings to the file.

# write to file
file.write("This is the first line.\n")
file.write("This is the second line.\n")

# write list of strings to file
lines = ["This is the third line.\n", "This is the fourth line.\n"]
file.writelines(lines)

Example: Writing to a Text File

Suppose we want to write the following text to a file called "output.txt":

This is the first line.
    This is the second line.

To write this text to the file, we would use the following code:

file = open("output.txt", "w")
file.write("This is the first line.\n")
file.write("This is the second line.\n")
file.close()

Closing Text Files

It's important to close a file when we're done with it, especially if we're writing to it. If we don't, the file may not be saved properly. To close a file, we can use the close() function.

file = open("filename.txt", "r")
# read file contents
file.close()

Example: Closing a Text File

Suppose we want to read the contents of a file called "input.txt" and print them to the console. Then, we want to close the file.

file = open("input.txt", "r")
contents = file.read()
print(contents)
file.close()

Using 'with' Statements

Another way to open and close text files in Python is to use a with statement. This ensures that the file is closed properly, even if an error occurs.

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

The above code opens the file in read mode and assigns it to the variable file. The with statement ensures that the file is closed when the block is exited. We can also use the same syntax for writing to a file.

Example: Using 'with' Statements

Let's modify the previous example to use a with statement:

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

Conclusion

Python provides several ways to read and write text files. We can use open() and close() to manually open and close files, or we can use with statements to ensure the file is closed properly. Remember to use "r" for reading a file and "w" for writing to a file. Also, always remember to close a file when we're done with it.


Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top