Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Reading From a File

Give Feedback

Once you’ve opened up a file, you’ll want to read or write to the file. First off, let’s cover reading a file. There are multiple methods that can be called on a file object to help you out.

read() takes a number as a parameter and reads from the file based on that number of bytes. If no argument is passed, or None or -1 is passed, then the entire file is read.

readline() takes a number as a parameter and reads at most that number of characters from the line. This continues to the end of the line and then wraps back around. If no argument is passed, or None or -1 is passed, then the entire line (or rest of the line) is read.

readlines() reads the remaining lines from the file object and returns them as a list.

00:01 Reading from a file.

00:06 The .read() method reads the contents of an open file as a number of bytes, here given as an argument to the .read() method. Let’s see that in action.

00:18 Here is the text file we’ll be working with. As you can see, it consists of three lines of text: the first line, the second line, and the last line. Now switching to the editor, we can create the code we saw earlier.

00:33 So with open() and the filename as file: content = file.read() and then the number of bytes, which is 10 in this case. And now we’ll just print that content out.

00:52 Let’s save that, and then run it.

00:59 And here we can see those 10 bytes printed out, This is th.

01:05 It is of course possible to change the number of bytes taken in each slice. So here, we’re going to read 5 bytes, and there you see This and of course a space, which is invisible.

01:20 The .readline() method read a single line from the file. Here we can see the form is almost identical with the method just changing to .readline() and no arguments given in this case.

01:30 Let’s see that in action. As you can see here, we have the same code as before, which is just going to be modified to use .readline() instead of .read().

01:42 And when you see that run, the entire first line is read from the file. It’s possible to repeat the .readline() method until the file is exhausted.

01:54 So here, we should see another line. On running it, we see This is the first line. and This is the second line. The .readlines() method reads the contents of the file as a list.

02:08 So now our content variable will contain a list of the lines in text_file.txt. Let’s see that in action. As you can see, this is the previous script, and now the .readline() method will be altered to .readlines().

02:24 On running, printing out the content variable we can see we get a list of all of the lines within that file object.

02:34 So far, we’ve only been reading content from a file, and the default mode of open() is to open the file for reading. There’s no argument given, as seen here. However, it is possible to give the read argument explicitly.

02:51 It’s functionally equivalent to the first, but we’ve explicitly stated that the file is being opened in read mode ("r"), which can be easier to understand when reading through code later on.

03:03 However, there is more to files than just reading from them.

Shawn Bardong on Feb. 16, 2020

Appreciate the tutorial, but in which directory are you putting the text file? you need to run the script in the same directory as the text file? How should you separate your data and scripts into different directories/folders?

khurram703 on June 13, 2020

if u pass None as an argument to the readline function, it raises an exception!it will not read the entire line

Become a Member to join the conversation.