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.
Pingback: Super Simple Python – Input - TorelTwiddler