Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Reading From a File

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.

Avatar image for Shawn Bardong

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?

Avatar image for khurram703

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

Avatar image for trevorrigden

trevorrigden on March 24, 2024

This is a poke in the dark. I am still not proficient with python but am soldiering on. I work on window 11 computers. when opening or using open() command the objext i get back is a TextIOWrapper item. as these read then close it leaves an empty list. I may be asking a little too much or maybe not enough. I throw my self into the deep end. yes the files do work in some scripts but others leave the error.

you are aware of the advent of code. day 8 part 2 asks for ‘Now, let’s go the other way. In addition to finding the number of characters of code, you should now encode each code representation as a new string and find the number of characters of the new encoded representation, including the surrounding double quotes.”

the arepl that i use has the item as it is on the computer but i can not replicate this in code because of TextIOWrapper. I do not have enough knowledge of this. The actual arepl shows the file to count the requested items but i would like a tutorial on the textIOwrapper, ido not wish to go down the path of linux or ubuntu. there must be some work around this item. as a note, the data that is read by day 8 input file 2015 is the required data for computation but windows works in a way that i do not know. Help.

Become a Member to join the conversation.