Python – text files

Outside Resources:

https://docs.python.org/3/library/functions.html#open

https://www.guru99.com/reading-and-writing-files-in-python.html

https://www.w3schools.com/python/python_file_handling.asp

https://docs.python.org/3/library/io.html


Creating a File

f = open('my_file.txt', 'w+')
f.write('Title of my text file\n')
f.write('End of my file')
f.close()

This example opens the file, writes to it twice, then closes the file.

The “w+” option means you are wiping out the contents of the file if it already exists (“w”) and that you are writing to something on a disk (“+”).

Notice that the text contains a line break character (“\n”).

If you want to add (“append”) to an existing file, you can use the “a” option instead:

f = open('my_file.txt', 'a+')
f.write('More text to add to my file\n')
f.write('This is some pretty solid info.')
f.close()

Reading a File

If you already have a text file, python can create a file object in memory and then read its lines.

Let’s say you already have a file named dogs.txt that contains the following data:

spaniel
collie
lab
terrier
mutt

(If you want to try this example, you will need to create the dogs.txt file and paste in the data. The file must be saved in your working directory.)

To open file and print every line:

f = open('dogs.txt')
data = f.read()  # reads as one big string
dogs = data.split('\n')
print(dogs[0])  # print a couple of dogs
print(dogs[1])

There is also a readlines() function that already splits the lines:

f = open('dogs.txt')
data = f.readlines()
print(data)
f.close()

You are opening the file, then using readlines() to create a list of the file’s lines called data.

There is also a function, read(), that reads the entire file as one big string.

You can easily print each line individually:

f = open('dogs.txt', 'r')
data = f.readlines()
for line in data:
    print(line)
f.close()

You will notice that Python prints a blank line between each dog. This is because each line contains a line break character, “\n”, at the end of the line. You can use strip() to get rid of white space at the start and end of each line (includes spaces, tabs, and line breaks):

f = open('dogs.txt', 'r')
data = f.readlines()
for line in data:
    print(line.strip())
f.close()

Very often it is useful to store the lines of data into a list, but without the whitespace (line breaks):

f = open('dogs.txt', 'r')
data = f.readlines()
dogs = []
for line in data:
    dogs += [line.strip()]
f.close()

Now you have a list stored in memory called dogs that is ready to be used in your code.

If you want to be fancy, you can use a list comprehension to do the loop in one line of code. If you look for file examples online, you might encounter this type of solution:

f = open('dogs.txt', 'r')
data = f.readlines()
dogs = [line.strip() for line in data]
f.close()

You can shorten this code further using a few more tricks.

with open('dogs.txt') as f:
    dogs = [line.strip() for line in f.readlines()]
  • Using the ‘with open’  syntax causes python to automatically close the file after the indented code finishes.
  • The ‘r’ option can be omitted from the open command since ‘r’ (read only) is the default option.
  • We skipped “data = f.readlines() and put “f.readlines()” directly into the list comprehension to create the list with one line of code.