Our examples so far that have touched the filesystem have operated entirely on text files without much thought as to what is going on under the hood. Operating systems represent files as a sequence of bytes, not text. We’ll take a deep dive into the relationship between bytes and text. For now, be aware that reading textual data from a file is a fairly involved process, but Python takes care of most of the work for us behind the scenes.

The concept of files has been around since long before anyone coined the term object-oriented programming. However, Python has wrapped the interface that operating systems provide in a sweet abstraction that allows us to work with file (or file-like, vis-a-vis duck typing) objects.

The confusion arises because the operating system file and the Python file object are both, commonly called files. It’s difficult to be ultra-cautious and wrap each reference to the term file with appropriate context to distinguish bytes on a disk from the OS libraries for accessing those bytes from the Python file object that wraps the OS libraries.

Opening a file

Python’s open() built-in function is used to open the OS file and return a Python file object. For reading text from a file, we only need to pass the name of the file into the function. The OS file will be opened for reading, and the bytes will be converted to text using the platform’s default encoding.

Mentioning the path

A file name can be a name relative to the current working directory. It can also be an absolute name, beginning from the root of the directory tree. A file’s name is the tail end of a path to the file from the root of the filesystem. The root in a Linux-based filesystem is /. In Windows, there’s a filesystem on each device, so we use a more complex name like C:\. While Windows uses \ for separating elements of the file path, Python’s pathlib uses / consistently, converting the string to the OS-specific names when needed.

Writing in a file

Of course, we don’t always want to read files; often, we want to write data to them. To open a file for writing, we need to pass a mode argument as the second positional argument to open(), with a value of "w":

Get hands-on with 1200+ tech skills courses.