In this lesson, you will apply what you have learned in the previous lessons to read and write data into files. In addition to applying what you have learned in the previous courses, you will also build on your knowledge of the open() built-in function and learn more about this function to write data into a file.

Not only will you learn how to read and write files, but you will also learn how to perform those actions using the appropriate encodings. You will also learn how to use context managers for opening files, which makes it not necessary to manually close files once we are done working with them.

You will also learn two important workflows:

  1. How to identify the encoding of a file.
  2. How to convert a file from one encoding to another.

Learning how to read and write files using the appropriate encoding is important. If the incorrect encoding is used, your file may not be readable or writable. For example, as a data scientist or data engineer, using the wrong encoding to read a file may result in an unintelligible analysis or a wonky data pipeline built.

As you work through this lesson and learn about reading and writing to files, you’ll get to apply what you’ve learned from within your browser; there's no need to use your own machine to do the exercises. The Python environment inside of this course includes answer-checking to ensure you've fully mastered each concept before learning the next.


  • About different modes of opening files.
  • About context managers.
  • How to deal with different encodings when reading from files.
  • How to write to files.
  • How to convert between encodings.

Lesson Outline

1. Reading Files in Python
2. Reading with a Context Manager
3. Reading with the Wrong Encoding
4. Finding the Right Encoding
5. Reading with the Right Encoding
6. Encoding Identification Workflow
7. Writing to a File
8. Appeending to a File
9. Converting the Dataset to UTF-8
10. Next Steps
11. Takeaways