Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

Python Basics: Reading and Writing Files (Overview)

Files are everywhere in the modern world. They’re the medium in which data is digitally stored and transferred. Chances are, you’ve opened dozens, if not hundreds, of files just today! Now it’s time to read and write files with Python.

In this video course, you’ll learn 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

This video course is part of the Python Basics series, which accompanies Python Basics: A Practical Introduction to Python 3. You can also check out the other Python Basics courses.

If you’d like to review file operations before diving into this course, then check out Python Basics: File System Operations.

Note that you’ll be using IDLE to interact with Python throughout this course. If you’re just getting started, then you might want to check out Python Basics: Setting Up Python before diving into this course.

Download

Sample Code (ZIP)

2.8 KB
Download

Course Slides (PDF)

9.6 MB

00:00 Welcome to Python Basics: Reading and Writing Files.

00:05 Files are abundant in the modern world. They’re the medium in which data is digitally stored and transferred. Chances are you’ve opened dozens, if not hundreds, of files just today.

00:18 In this course, you’ll learn how to move data back and forth between your Python programs and external software by reading and writing files. More specifically, you’ll be working with human-readable text files, which contain plain text as opposed to binary data meant for machines to process. Along the way, you’ll practice reading and writing data stored in the CSV file format, which stands for comma-separated values.

00:45 It’s one of the most widely supported file formats for transferring tabular data, such as spreadsheets or database tables. Data scientists and machine learning engineers often choose CSV to store and exchange datasets too.

00:59 The sample file that you can see here is a valid CSV file representing fake home budget expenses that you’ll use later in this course. You can download it by expanding the Supporting Materials dropdown just below this video and choosing the Sample Code archive.

01:18 Real Python has several great tutorials and video courses dedicated to handling CSV files in Python, so feel free to check them out later.

01:30 Before you get started, here’s a quick overview of the topics that you’re going to cover over the next few lessons. First, you’ll get a general idea about the differences between text and binary files, which are the two primary types of files on any computer. However, in this course, you’ll mostly focus on reading and writing text files.

01:50 In Python, reading text files requires you to choose the right character encoding and line endings, or else you may end up with garbled text. You’ll also take a closer look at the file object in Python, which represents the file on disk and provides methods for reading and writing data to it. Then you’ll learn about several file modes, which can restrict the available operations on the same file, depending on how you want to use it.

02:16 You’ll open the file in Python using a few different approaches, and you’ll learn about some useful idioms to avoid problems when reading and writing files. Finally, you’ll take advantage of the standard library’s csv module to manipulate tabular data that you might have exported from a spreadsheet program.

02:35 Note that this video course is a part of the Python Basics learning path based on the book with the same title, which you can get at the link below. While this course is reasonably self-contained, you’ll benefit from watching an earlier course entitled File System Operations or reading the corresponding chapter in the Python Basics book, which provides the foundation for the material covered in this course.

02:58 You’ll find the links to all these resources in the description below.

03:05 Like all the other courses in this Python Basic series, this one also uses IDLE to demonstrate code snippets. IDLE, or the Integrated Development and Learning Environment, should already come with your Python distribution, so there’s no extra step for you to take.

03:21 You can run it by typing idle at your terminal prompt, or by finding the IDLE icon in your operating system applications.

03:31 If you’re unsure what IDLE is, check out an earlier course in the Python Basics series entitled Setting Up Python. You can also watch a separate Starting With Python IDLE video course, which goes into even more detail.

03:47 This video course is made up of the following lessons. You can stop the video at this point and take some time to become acquainted with a table of contents before moving on. All right, it’s time to crack open your first file in Python and see what’s inside.

Sneha Nath on Sept. 9, 2023

Excellent Course!! Thank you.

Bartosz Zaczyński RP Team on Sept. 11, 2023

@Sneha Nath You’re welcome! I’m glad that you enjoyed the course 😊

Become a Member to join the conversation.