Welcome to the first article in the Series "Python and Files" where I will be using Python to open, read and write different file formats.
In this episode, We will be reading and writing text files in Python.
Python provides in-built standard libraries that are capable of writing and reading text files
Reading a TXT file in Python
There are different ways we might want to read our text files, these are some of them.
Read the entire text file all at once
with open('file.txt', 'r') as file:
contents = file.read()
print(contents)
First, we use the open() method to open the file in our program and we use the "r" to signify that we are going to be reading the file.
Then we save the content of the file to a variable with the method "read()"
Read line by line
with open('file.txt', 'r') as file:
contents = file.readlines()
for i in contents:
print(i)
Writing a TXT file in Python
Similarly to reading a text file, Writing a text has multiple methods, and below are some of those methods
Write the entire text file at once
with open('file.txt', 'w') as file:
file.write('Hello, world!')
In this case, we use the "w" to signify that we are going to be writing to the file.
Then we write to the file by using the "write() method"
Write line by line
lines = ['Hello', 'world', 'how', 'are', 'you']
You can use 'a' in place of 'w' when writing to an existing file ('a' stands for append)
with open('file.txt', 'w') as file:
file.writelines('\n'.join(lines))
That's all for "Exploring .txt file with Python"
Follow to get notified when new articles drop
You can reach me on Twitter 🐦
Top comments (0)