CHAPTER 4
Working with Files and Directories
In this chapter, you learn to use Python to work with files and directories. You start by learning the essentials and then move on to navigating between directories and working with them. You learn how to return information about the user and system and how to split a file path into its components. And you gain expertise in opening and closing text files, writing data to them, and reading their contents.

Understanding Working with Files and Directories
Load the os Module and List Files and Directories
Rename, Move, and Copy Files and Directories
Get Information About the User and System
Split a File Path into Its Components
Understanding Python’s open() Function
Understanding Python’s Ways of Closing Files
Open a File If It Exists; If Not, Create It
Check an Open File’s Status and Close It
Understanding Working with Files and Directories
This section gives you an overview of how you work with files and directories in Python. To make sure you are clear on the essentials, we first cover what files and directories are and what directory paths and file paths consist of. We then introduce you to three key modules you will need to load at different points during this chapter, briefly ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access