Python Bites: Manipulating Text Files | by Soner Yıldırım | Towards Data Science

Text files are commonly used to store data. Thus, they are essential in the data science system. Python provides versatile functions and methods to handle text file.
There are many options to create a text file. We will not cover all of them since the focus here is to manipulate text files, not creating them. Let’s go with a method that you can use in a jupyter notebook.
%%writefile myfile.txtPython is awesomeThis is the second lineThis is the last line
We now have a text file named “myfile.txt” in the current working directory. In order to read the content of a text file, we first need to open it.
f = open("myfile.txt")
We can now read the content of “myfile.txt”.
f.read()'Python is awesome\nThis is the second line\nThis is the last line'
The read function, as its name suggests, reads the content of the file. You may have noticed that the new lines are indicated with “\n”.
It is possible to read the content of the file line by line using the readlines function. It returns a list that contains each line as an item.
f.readlines()[]
Our readlines function returns an empty list which is a bit of a problem. The reason is that once you read a file, the cursor goes until the end. The next time you try to read the same file, the cursor cannot find anything to read because it is already at the end of the file.
The solution is to set the cursor back to the initial position. Then, we will be able read the file again.
f.seek(0)0f.readlines()['Python is awesome\n', 'This is the second line\n', 'This is the last line']
We should close a file once the content is read. Otherwise, we may encounter unexpected results.
f.close()
If you try to read a file after it’s closed, Python raises a value error indicating an I/O operation on a closed file.
We can also write to an existing file. The mode parameter is used for selecting the type of operation. The default value is ‘r’ which stands for read. Thus, we do not need to specify the value of mode parameter when reading a file.
If you want to overwrite the content, the mode is set as ‘w’. It is also possible to add new content to an existing file which is done by the append option (‘a’).
We will also go over examples that demonstrate how to overwrite a file or append new content. Before going into that part, I’d like to introduce a new method of opening files.
It may sound like a tedious operation to close the file each time you read it. Luckily, Python provides a more practical way of reading and writing files.
with open("myfile.txt", mode='r') as f:    content = f.read()print(content)Python is awesome This is the second line This is the last line
We do not need to explicitly call the close method on the file anymore. Let’s add a new file to our file. We should select the append option.
with open("myfile.txt", mode='a') as f:    f.write("\nTHIS IS THE NEW LINE")
Here is the new content.
with open("myfile.txt", mode='r') as f:    updated_content = f.read()print(updated_content)Python is awesome This is the second line This is the last line THIS IS THE NEW LINE
The write mode overwrites a file. Thus, it deletes what is already in the file and writes the new content.
with open("myfile.txt", mode='w') as f:    f.write("THIS WILL OVERWRİTE THE EXISTING LINES")
Let’s read the new content.
with open("myfile.txt", mode='r') as f:    updated_content = f.read()print(updated_content)THIS WILL OVERWRİTE THE EXISTING LINES
The mode parameter also accepts arguments that allow multiple operations. For instance, “r+” opens a file for both reading and writing.
with open("myfile.txt", mode='r+') as f:    content = f.read()    f.write("\nHere is a new line")    f.seek(0)    updated_content = f.read()
We open the file and read its content. Then we write a new line and read the updated content.
print(content)THIS WILL OVERWRİTE THE EXISTING LINESprint(updated_content)THIS WILL OVERWRİTE THE EXISTING LINES Here is a new line
We have done a few simple examples to show how Python handles reading and writing text files. There is, of course, more to it. This article can be considered as an introduction that will get you familiar with the operations.
It is always important to have a comprehensive understanding of the basics. You will gain advanced skills by building up your knowledge of the basics.
Thank you for reading. Please let me know if you have any feedback.