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

Read Starship Names (Solution)

00:00 Solving this exercise should take less time than before because some of the steps needed to complete it actually overlap with the ones that we’ve already covered in the previous exercise.

00:12 More specifically, reading a file in Python isn’t all that different from writing to it. You need to first open it using the write mode, so you can initially repeat the same code that you just wrote.

00:24 Start by importing the Path object, and then specify the actual path to your home directory, followed by the name of the file which you created in the previous exercise.

00:35 That is starships.txt.

00:39 You can assume this file exists, and you can open it using the familiar with statement, but this time you want to open it for reading, so you’ll use the letter "r" instead of the letter "w" as the value for the mode parameter.

00:53 If you’re a good citizen and you write, then you might as well specify the character encoding as UTF-8. Finally, you want to print each line in the file onto the screen.

01:06 One way to do this is by looping over the lines returned by the .readlines() method, exposed by the file object.

01:15 You can then print each line to the screen. Even though this looks correct, you should always verify your code through testing. So let’s save this file as a Python module named exercise_01_02, and let’s run it.

01:34 Okay. We can see the starship names appear correctly, but there are unnecessary blank lines between them, which goes against the last point in the exercise instructions.

01:46 At this point, you might remember it’s the print() function that adds an extra newline character at the end of each line by default. We can disable it by specifying an empty string as the value for the end parameter.

01:59 Let’s save the file and rerun it to see if that helps.

02:03 Indeed, that helps. Now that we’ve verified the code works as expected, we can think if there’s anything we can improve about it. For example, the .readlines() method is greedy, which means that it loads the entire file and splits it into separate lines stuffed into a Python list.

02:21 That could potentially allocate a lot of memory when your file is large. It may be more efficient to process it line by line instead of all at once like this.

02:31 In such a case, you can take advantage of the fact that file objects in Python are iterable themselves. If you iterate over the file without calling .readlines(), then Python will keep feeding you with the next line from the file until there are no more lines or you decide to break out of the loop prematurely.

02:51 This should still produce the same result, but use much less memory, which can be especially helpful when you work with those larger files. But since the starships file is tiny, we can actually read it all at once into a Python string instead of a list of lines.

03:09 The file object has a .read() method, which does just that. So instead of printing a line, we can print the whole file Again, this should work as before.

03:22 This simplifies the code even more, making me quite satisfied. I actually like this solution the best. What about you? Did you arrive at something similar?

Become a Member to join the conversation.