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

Python Basics: Reading and Writing Files (Summary)

Congratulations! Now you know how to read and write plain text files using the Path.open() method and built-in open() function, as well as how to work with comma-separated value, or CSV, files using the Python standard library’s csv module.

In this video course, you’ve learned how to:

  • Understand the difference between text and binary files
  • Learn about character encodings and line endings
  • Work with file objects in Python
  • Read and write character data in various file modes
  • Use open(), Path.open(), and the with statement
  • Take advantage of the csv module to manipulate CSV data

To continue your learning journey, check out these video courses:

You might also enjoy these written tutorials:

Finally, you can revisit other topics in the Python Basics learning path.

Download

Sample Code (ZIP)

2.8 KB
Download

Course Slides (PDF)

9.6 MB

00:00 In this lesson, you’ll briefly recap what you’ve learned about reading and writing files in Python. You’ll also check out some links with additional resources to help you deepen your understanding of these topics. Finally, you’ll have a chance to challenge yourself with a few practical exercises.

00:18 You started this course by learning how to open a file in text and binary modes in Python and why it’s important to close the file when you’re done working with it. While taking a closer look at text files, you discovered the concepts of character encodings and line endings and learned some best practices around them. Along the way, you worked with file objects, which are Python’s representation of files stored on your computer.

00:43 They expose several different operations determined by a particular file mode that you choose when you open a file. You also looked at several modes in which you can read and write text files in Python, including read-only mode, write-only mode, and append-only mode.

00:59 You used the traditional as well as slightly more modern ways of handing files in Python, which ensure that the allocated resources are properly cleaned up even in the event of errors. Finally, you took advantage of the csv module that ships with Python to read and write text files written in the popular comma-separated values file format.

01:21 If you’re thirsty for more, then here are some additional resources to help you with your learning journey. You can find the links to these tutorials and video courses in the description below, as well as on the attached slides. To get them, click the Supporting Materials dropdown and choose the resource to download that might interest you.

01:42 Now it’s time for a little challenge. You can stop this video at each exercise and resume once you’ve solved it. The goal of the first exercise is to write a few lines of text into a file located in your home directory.

02:00 The second exercise depends on the previous one, so make sure you solve them in order. Here, you’ll reverse the process by reading those few lines from the file and printing them back onto the screen.

02:15 In the third exercise, you are asked to save the following Python dictionary in a comma-separated values file. Make sure to include the file header. This is the expected file contents.

02:32 In the last exercise, you’ll reverse the process again by loading the data from a CSV file into a Python dictionary.

02:43 That’s it. Thanks for your attention, and see you in the next one.

Avatar image for toigopaul

toigopaul on Jan. 2, 2024

In exercise 3, the expected file contents has “favorite color” the second field of the first line. It should be “favorite_color”.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Jan. 3, 2024

@toigopaul Ah, good catch. Thanks!

Become a Member to join the conversation.