12 Using the filesystem

 

This chapter covers

  • Managing paths and pathnames
  • Getting information about files
  • Performing filesystem operations
  • Processing all files in a directory subtree

Working with files involves one of two things: basic I/O (described in chapter 13) and working with the filesystem (for example, naming, creating, moving, or referring to files), which is a bit tricky, because different operating systems have different filesystem conventions.

Since we are assuming Google Colaboratory, which is hosted on Linux, as our default environment, most of the examples in this chapter will be based on Linux. It’s quite possible, however, that you will want to write scripts that access files that run on other platforms, so we will include mention of how the same operations work on those platforms, particularly Windows, as needed.

It would be easy enough to learn how to perform basic file I/O without learning all the features Python has provided to simplify cross-platform filesystem interaction—but I wouldn’t recommend it. Instead, read at least the first part of this chapter, which gives you the tools you need to refer to files in a manner that doesn’t depend on your particular operating system. Then, when you use the basic I/O operations, you can open the relevant files in this manner.

12.1 os and os.path vs. pathlib

12.2 Paths and pathnames

12.2.1 Absolute and relative paths

12.2.2 The current working directory

12.2.3 Accessing directories with pathlib

12.2.4 Manipulating pathnames

12.2.5 Manipulating pathnames with pathlib

12.2.6 Useful constants and functions

12.3 Getting information about files

12.3.1 Getting information about files with scandir

12.4 More filesystem operations

12.4.1 More filesystem operations with pathlib

12.5 Processing all files in a directory subtree

12.6 More file operations

12.6.1 Solving the problem with AI-generated code

12.6.2 Solutions and discussion

Summary