File Handling in Python

In this tutorial, I will show how to handle files in Python.

Create, write, read, append and close files.

File create

First things first, create!

We are going to use the open() function.

This function opens a file and returns its corresponding object.

The first argument is the name of the file we are handling, the second refers to the operation we are using.

The code below creates the file "people.txt", the x argument is used when we just want to create the file. If a file with the same name already exists, it will throw an exception.

people_file = open("people.txt", "x")

You can also we the w mode to create a file. Unlike the x mode, it will not throw an exception since this mode indicates the writing mode, that is, we are opening a file to write data into it and, if the file doesn’t exist, it is created.

people_file = open("people.txt", "w")

The last one is the a mode which stands for append. As the name implies, you can append more data to the file, while the w mode simply overwrites any existing data.

When appending, if the file doesn’t exist, it also creates it.

people_file = open("people.txt", "a")

File write

To write data into a file, you simply open a file with the w mode.

Then, to add data, you use the object return by the open() function, in this case the object is called people_file, and then call the write() function passing the data as argument.

people_file = open("people.txt", "w")
people_file.write("Bob\n")
people_file.write("Mary\n")
people_file.write("Sarah\n")
people_file.close()

We use \n at the end to break the line, otherwise the content in the file would stay int the same line like "BobMarySarah".

One more detail is to close() the file, this is not only a good practice, but also ensures that your changes were actually applied to the file.

Remember that when using w mode, the data that already existed in the file will be overwritten by the new data, to add new data without losing what was already there, we have to use the append mode.

File append

The a mode appends new data to the file keeping the existing one.

In this example, after the first writing with w mode, we are using the a mode to append and the result is that each name will appear twice in the file "people.txt".

#first write
people_file = open("people.txt", "w")
people_file.write("Bob\n")
people_file.write("Mary\n")
people_file.write("Sarah\n")
people_file.close()

#appending more data
#keeping the existing data
people_file = open("people.txt", "a")
people_file.write("Bob\n")
people_file.write("Mary\n")
people_file.write("Sarah\n")
people_file.close()

File read

Reading the file is also very straightforward, just use the r mode like so.

If you read the "people.txt" file created in the last example, you should see 6 names in your output.

people_file = open("people.txt", "r")
print(people_file.read())
#output:
#Bob
#Mary
#Sarah
#Bob
#Mary
#Sarah

The read() function reads the whole file at once, if you use the readline() function, you can read the file line by line.

people_file = open("people.txt", "r")
print(people_file.readline())
#Bob
print(people_file.readline())
#Mary
print(people_file.readline())
#Sarah

You can also a loop to read the lines like the example below.

people_file = open("people.txt", "r")
for person in people_file:
  print(person)
#Bob
#Mary
#Sarah
#Bob
#Mary
#Sarah

That’s it!

This is the basics of handling files in python, I will also make a post for binary files and handling binary data.