7) a. Reading and Writing Files

Improved string concatenation:

Before we proceed any further it is important that you become acquainted with an advanced usage of concatenating strings together. This is essential because it will make your life much easier when you’re writing to files because it involves a lot of strings.

So far you’ve gained a comfort zone in adding strings with variables together with the + sign. For example:

1
2
string1 = "some text " + "with more text" + str(2)
string2 = "converting numbers to string so that you can concatenate. For example: " + str(20)



However, there is an easier way to concatenate strings together with variables that you didn’t know! You can use curly braces {} inside a string. Then, at the end of the string, just supply the missing pieces by calling the format function. Here’s an example:

1
2
The Answer to the Great Question... Of Life, the Universe and Everything... Is... 42,' said Deep Thought, with infinite majesty and calm.
― Douglas Adams, The Hitchhiker'
s Guide to the Galaxy



As you can see, the string’s format function takes the missing pieces and plugs them up wherever you you’ve inserted curly braces{}. This is very useful because you don’t have to bother about converting integers to string, the + signs, closing and opening the quotes. Isn’t this easy?

Now that you know how to concatenate strings, we can get started with reading and writing files!


Reading and writing files:

Like most things in Python the basics of file handling are very simple but can be expanded upon if there is a need.

Here is what the syntax of opening a file looks like in Python:



Here is a low-level display of reading a file and printing its contents.

1
2
3
4
5
6
7
8
file = open("some_text_file.txt", "r")

one_line = file.readline() #reads only 1 line
remaining_lines = file.read() #reads all subsequent lines

print(text)

file.close()



First of all you open the file. Then you read its contents. You can actually specify how many characters you want to read, but by default you read the whole file. After that you print the contents. And finally you have to close the file so that other programs can have access to it.

What if you wanted to write to a file instead of reading it. You would have to change the access mode in that case. Notice that in the previous example, in the open statement, we have passed the character “r” which stands for reading a file. You could change this to “w” if you want to write to the file.

Here are the different access modes available in Python:



If you look carefully at this image you will notice that there are 4 compulsory modes out of which you need to pick one. Earlier we had picked “r” so that we could read from a file. We will now pick “w” and write to it.

1
2
3
4
5
6
7
8
file = open("to_do_list.txt", "w")

file.write("1. Get some eggs\n")
file.write("2. Get some bacon\n")
file.write("3. Cook and eat them\n")
file.write("4. Watch that movie my friend was recommending\n")

file.close()



Since you’ve used a write mode your file will get created if it doesn’t exist. In case of a read mode, it would throw an error if the file doesn’t exist. That’s because you can’t read from a file that doesn’t exist.

Now that you’ve created your to-do list you want to add some more to-dos for tomorrow.

However, if you open the existing file again using the mode “w” then it will overwrite all existing data. In other words, all existing data will be erased and Python will write from the start of the file.
To prevent this from happening, you should open the file in the “append” mode which is represented by “a”.

Here’s how you would go about doing that:

1
2
3
4
5
6
7
file = open("to_do_list.txt", "a")

file.write("1. Meet John\n")
file.write("2. Visit the Iceberg cafe and try their new hot dog\n")
file.write("3. Attend the marketing seminar in the evening\n")

file.close()



What if you first wanted to read your existing to-do list and then append to it? When you open the file in append-mode you cannot read.

Fortunately, Python provides optional modes that can be combined with either of the four compulsory modes. In this case we would like to use the optional “+” mode along with “a”.

The “+” mode allows you to read as well as write to any file, irrespective of whichever mode you had used to opened the file in.


In other words, you have the following:

Append-modeEffect
rRead
r+Read and write
wWrite from start
w+Read and write.
Cursor at the start of the file.
aAppend (Write from end of file, aka keep existing data).
a+Read and write.
Cursor at the end of the file.


So what if you want to first print out the existing to-do entries in your file before you writing? You could open your file in “r+” mode which would have your file cursor at the beginning of the file by default and start reading.

Alternatively, you could open the file with “a+” mode and then try to move your file cursor to the beginning of the file.

So how do you read from a particular position in a file? Well, you can use the seek method for that.

1
2
3
4
5
6
7
8
9
10
11
12
file = open("to_do_list.txt", "a+")

#This takes your file cursor to the start of the file
file.seek(0)

#Read from the file which will bring your cursor to the end
print(file.read() )

#Write to the file
file.write("That's all for today!")

file.close()



Sounds straight forward? On the surface this approach is enough. However many things can happen between opening and closing the file that may not be under your control. Therefore it is up to you to constantly check if the file still exists or available or many other states that it can have. But you don’t want to do it by yourself.

That’s why you can use a context manager called with to help you.

1
2
3
with open("to_do_list.txt","r") as file:
    text = file.read()
    print(text)



As you can see the syntax is a little bit different. But it is more readable and most importantly more convenient. There is no more close function being called because it is done automatically at the end of the with‘s body.

In fact close will be called even if something in the body goes wrong. That way you never block access to the file.

This is the recommended way to process files in Python.



Here is a summary of the important methods when you want to read / write to files: