• $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
1. Files, filesystems, and file objects
Exercise files

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
2. Opening files
3. Absolute and relative paths
Preview
4. Exceptions
5. Reading from files
6. Iterating over files
7. Filtering
8. Comprehensions
9. Exercise #1
10. Exercise #1 solutions
11. Closing files
12. Using "with"
Preview
13. Closing files in "for" loops
14. Exercise #2
15. Exercise #2 solutions
16. os.listdir and os.path.join
17. glob.glob

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
18. Writing to files
19 Appending with "a"
20 Line-buffered output
21 print and sys.stdout
Preview
22 sys.stderr and redefining sys.stdout
23 "with" and multiple files
24 Exercises 3
25 Exercise 3 solutions

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
26 Unicode and encodings
27 Specifying an encoding
28 Strings and bytestrings
29 Reading and writing bytes
30 Identifying JPEGs by their bytes
31 Exercises 4
32 Exercise 4 answers

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
33 Moving around with "seek" and "tell"
34 Working with r+, w+, and a+ modes
35 Newlines
36 sys.stdin
37 os.path
38 StringIO
39 os.walk
40 Exercises 5
41 Exercise 5 solutions

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
42 pickle
43 csv
44 json
45 Exercises 6
46 Exercise 6 solutions

Section 6: Pathlib

Section 6 — Pathlib.ipynb
47 pathlib
48 pathlib and directories
49 pathlib and files
50 Exercises 7
51 Exercise 7 solutions

Conclusion

52 Conclusion