Salvatore Lopiparo

Software Engineer

Super Simple Python #16 – Reading Files

Last lesson we learned about writing to a file, this time we will read from it as well.

If this is your first time here, I recommend looking at the first lesson. Starting there and going through the rest of the lessons will prepare you to go through this more advanced lesson!

For our lesson, let’s create a file with some information in it. Create a file somewhere (in this example, it’s at C:/Users/Salvatore/Documents/raven.txt) and type in a few lines of information that we can retrieve.

Once upon a midnight dreary, while I pondered weak and weary,
Over many a quaint and curious volume of forgotten lore,
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
"'Tis some visitor," I muttered, "tapping at my chamber door -
Only this, and nothing more."

Just as we opened the file for writing last time, we need to open it for reading. We use the 'r' mode to access the file in read mode.

>>> f = open('C:/Users/Salvatore/Documents/raven.txt', 'r')

Now that we have the file open, there are a few methods that we can use on f to get the data from the file.

read

The first method is the most straightforward. The f.read() method reads and returns the entire contents of the file as a string.

>>> raven = f.read()
>>> print(raven)
Once upon a midnight dreary, while I pondered weak and weary,
Over many a quaint and curious volume of forgotten lore,
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
"'Tis some visitor," I muttered, "tapping at my chamber door -
Only this, and nothing more."

One thing to be wary of when using read is that the entire contents of the file is read and put into memory. That means if you are reading a very large file (say multiple gigabytes), it will load it all into memory, which could lead you to very slow performance.

Another thing to note when using any of the read methods (or write methods) is that there is a pointer index that remembers the location to which the file has been read (or written). To see this, try reading from the file again:

>>> f.read()
''

It now returns an empty string because the index is at the end of the file, with nothing left to read! If we want to read from the beginning again there are two options: 1) close and open the file again, or 2) use the f.seek(0) method. The integer passed to f.seek indicates where to move the index to, 0 being the character at the beginning of the file, 1 being the character after the first, 2 the character after that, etc.

readline and readlines

The next commonly used methods of reading a file are f.readline() and f.readlines(). While read returns a single string of the entire file, readline returns a string of the first line separated by the newline character (‘\n’). Subsequent calls to readline will return the next line in the file.

>>> f.seek(0)  # To return to the beginning of the file
0
>>> f.readline()
'Once upon a midnight dreary, while I pondered weak and weary,\n'
>>> print(f.readline())
Over many a quaint and curious volume of forgotten lore,

Similarly, readlines returns a list of the same strings, also separated by newline characters.

>>> lines = f.readlines()
>>> for line in lines:
...     print(line)
...
While I nodded, nearly napping, suddenly there came a tapping,

As of some one gently rapping, rapping at my chamber door.

"'Tis some visitor," I muttered, "tapping at my chamber door -

Only this, and nothing more."

Note that we have an extra blank line between each phrase. This is because readline and readlines include the newline character at the end of each string, and using print adds a newline character itself. To avoid this issue, call the strip() method on each of the strings. This removes any extra whitespace characters from either end of the string.

>>> f.seek(0)
>>> for line in lines:
...     print(line.strip())
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
"'Tis some visitor," I muttered, "tapping at my chamber door -
Only this, and nothing more."

Using f.readline() with large files has the side effect over f.read() or f.readlines() of not loading the entire contents of the file into memory.

Super Simple Python #15 – Writing to Files

This week we will learn how to write to files! Writing to files is one of the simplest and most basic ways to communicate information, either to a user or to another program (or to the program itself!).

If this is your first time here, I recommend looking at the first lesson. Starting there and going through the rest of the lessons will prepare you to go through this more advanced lesson!

This first part is easiest to do in an interactive console.

Opening a file to either read or write is very simple: use the open built-in function. To use open, we must pass two arguments: the path of the file as a string (e.g. 'C:/directory/path/file_name.txt'), which you can get from using your computers file explorer (either Windows Explorer or Finder on a Mac); and an access mode which tells the program how to open the file. There are several access modes, but the most common are 'r' for read the file, 'w' for write to file, and 'a' for append to the end of the file. We’ll be writing in our examples this lesson.

This is how we use open with a path and an access mode:

>>> f = open('C:/test_file.txt', 'w')

A couple of things happen when we execute this line.

The first thing is that when we use 'w' to gain write access, Python will lock the file path that we’ve given it, so that it has exclusive rights to the file. Until we close the file or close Python, that file cannot be modified or deleted by any other program.

The next thing to note is that we get a file object back from open. This file object is what we will use to write stuff to the file.

To close our open file, we call the close method on the file object. This will allow other programs to get access to the file. In practice, you should only keep a file open as long as you need access to it.

>>> f.close()

Note that you cannot write to a file if it has been closed! You must re-open to access it again.

There are two common methods for writing to a file: write and writelines. The first method, write, takes any string you give it and writes it directly to the file.

>>> f.write("This is a string.")
>>> f.write("Here is another string.")

If you close the file using f.close() and look at it in a text editor (PyCharm will work! Just drag it in from your file browser), you will notice that they are on the same line:

This is a string.Here is another string.

If you use the write method, you need to handle new-line characters, or \n, yourself.

>>> f = open('C:/test_file.txt', 'w')
>>> f.write("This is a string.\n")
>>> f.write("Here is another string.\n")
>>> f.close()

You should see this in the file:

This is a string.
Here is another string.

The other method, writelines, handles the \n‘s for you, but you need to pass it a list of strings.

>>> lines_to_write = ["This string will be used with 'writelines'!", "This one will too!"]
>>> f = open('C:/test_file.txt', 'w')
>>> f.writelines(lines_to_write)
>>> f.close()

Now if we check the file:

This string will be used with 'writelines'!
This one will too!

That is really all there is to writing files. The only other interesting things that are used often are the read mode 'r', and the append mode 'a'. Append mode works just like write, except when you use write or writelines, it appends the text to the end of the file, which is very useful for logging information. You should play with it! Try running the same commands in this lesson with append mode 'a' instead of write mode 'w'.

We will handle read mode next lesson.