• $180

Intro Python: Files

Get this course, and many others, as part of my LernerPython membership, at https://LernerPython.com.

Everyone works with files

It's hard to imagine writing a program that doesn't use files in some way. Whether you're reading configurations, parsing logs, analyzing text, decoding JSON, or writing reports, you're working with files.

Python makes it easy to work with files and directories. Indeed, that's one of the reasons why Python is commonly used as a replacement for bash shell scripting — because of the ease with which you can work with, and navigate through, directories and files.

In this 5-hour video course, I walk you through the different ways in which you can work with files — from the simplest techniques to the most advanced and modern ones. We discuss a variety of issues you might encounter, including binary (byte) files, context managers, and Unicode characters.  I introduce several common file formats, such as CSV and JSON, with which you'll want to work. And a section at the end of the course walks you through "pathlib," Python's object-oriented interface to the filesystem.

As with all of my courses, I've included a large number of exercises that you can use to practice the ideas that I teach. And the Jupyter notebooks that I use when teaching the course are all available for you to download, experiment with, and use.

If you plan to use Python to read from or write to files, then this course will teach you what you need to get going.

Contents

Introduction

Welcome to the course!

What are files, file handles, and file objects?
Intro
  • 2 mins
  • 66.6 MB
1. Files, filesystems, and file objects
  • 6 mins
  • 124 MB
Exercise files
  • 747 KB

Section 1: Basic operations

In this section, you'll learn the basics of reading from files, and what you can do when you read from files.
Section 1 — Basic operations.ipynb
  • 184 KB
2. Opening files
  • 5 mins
  • 17.7 MB
3. Absolute and relative paths
  • 8 mins
  • 35.4 MB
Preview
4. Exceptions
  • 6 mins
  • 25.6 MB
5. Reading from files
  • 11 mins
  • 76.8 MB
6. Iterating over files
  • 7 mins
  • 155 MB
7. Filtering
  • 5 mins
  • 94.7 MB
8. Comprehensions
  • 3 mins
  • 12.5 MB
9. Exercise #1
  • 3 mins
  • 12.6 MB
10. Exercise #1 solutions
  • 8 mins
  • 35.4 MB
11. Closing files
  • 6 mins
  • 24.9 MB
12. Using "with"
  • 7 mins
  • 26.4 MB
Preview
13. Closing files in "for" loops
  • 3 mins
  • 58.4 MB
14. Exercise #2
  • 2 mins
  • 7.75 MB
15. Exercise #2 solutions
  • 6 mins
  • 130 MB
16. os.listdir and os.path.join
  • 7 mins
  • 153 MB
17. glob.glob
  • 7 mins
  • 145 MB

Section 2: Writing to files

In this section, you'll learn how to write to files, and how buffering can cause some problems that "with" can solve.
Section 2 — Writing to files.ipynb
  • 20.2 KB
18. Writing to files
  • 13 mins
  • 293 MB
19 Appending with "a"
  • 4 mins
  • 90.5 MB
20 Line-buffered output
  • 4 mins
  • 82.4 MB
21 print and sys.stdout
  • 6 mins
  • 121 MB
Preview
22 sys.stderr and redefining sys.stdout
  • 5 mins
  • 102 MB
23 "with" and multiple files
  • 5 mins
  • 99.1 MB
24 Exercises 3
  • 2 mins
  • 25.8 MB
25 Exercise 3 solutions
  • 6 mins
  • 134 MB

Section 3: Encoding and bytes

Python strings use Unicode, and Python expects files to be encoded with UTF-8. What if your file doesn't use Unicode? This section will show you what to do.
Section 3 — Encodings, byte strings, and binary files.ipynb
  • 29.6 KB
26 Unicode and encodings
  • 14 mins
  • 307 MB
27 Specifying an encoding
  • 9 mins
  • 207 MB
28 Strings and bytestrings
  • 9 mins
  • 191 MB
29 Reading and writing bytes
  • 7 mins
  • 142 MB
30 Identifying JPEGs by their bytes
  • 6 mins
  • 124 MB
31 Exercises 4
  • 1 min
  • 23.2 MB
32 Exercise 4 answers
  • 7 mins
  • 145 MB

Section 4: Advanced techniques

This section introduces a number of techniques that you probably won't need on a day-to-day basis, but that can be very helpful in certain circumstances.
Section 4 — Advanced techniques.ipynb
  • 4.95 MB
33 Moving around with "seek" and "tell"
  • 10 mins
  • 226 MB
34 Working with r+, w+, and a+ modes
  • 8 mins
  • 176 MB
35 Newlines
  • 10 mins
  • 215 MB
36 sys.stdin
  • 7 mins
  • 149 MB
37 os.path
  • 6 mins
  • 119 MB
38 StringIO
  • 4 mins
  • 78.5 MB
39 os.walk
  • 7 mins
  • 160 MB
40 Exercises 5
  • 1 min
  • 21.9 MB
41 Exercise 5 solutions
  • 12 mins
  • 261 MB

Section 5: Common file formats

Even though we're only working with simple files in this course, there are a number of formats in common use. In this section, we'll look at Pickle, CSV, and JSON, and how you can use them within Python.
Section 5 — Common file formats.ipynb
  • 65.6 KB
42 pickle
  • 10 mins
  • 223 MB
43 csv
  • 11 mins
  • 233 MB
44 json
  • 9 mins
  • 199 MB
45 Exercises 6
  • 1 min
  • 44 MB
46 Exercise 6 solutions
  • 7 mins
  • 153 MB

Section 6: Pathlib

Section 6 — Pathlib.ipynb
  • 72.1 KB
47 pathlib
  • 10 mins
  • 229 MB
48 pathlib and directories
  • 7 mins
  • 150 MB
49 pathlib and files
  • 7 mins
  • 161 MB
50 Exercises 7
  • 1 min
  • 17.2 MB
51 Exercise 7 solutions
  • 6 mins
  • 119 MB

Conclusion

52 Conclusion
  • 2 mins
  • 24.4 MB