Filesystem Paths

Learn about the filesystem paths and their implementation using a working example in Python.

We'll cover the following

Overview

Most operating systems provide a filesystem, a way of mapping a logical abstraction of directories (often depicted as folders) and files to the bits and bytes stored on a hard drive or another storage device. As humans, we typically interact with the filesystem using a drag-and-drop interface with images of folders and files of different types. Or we can use command-line programs such as cp, mv, and mkdir.

As programmers, we have to interact with the filesystem with a series of system calls. We can think of these as library functions supplied by the operating system so that programs can call them. They have a clunky interface with integer file handles and buffered reads and writes, and that interface is different depending on which operating system we are using. The Python os module exposes some of these underlying calls.

The path module

Inside the os module is the os.path module. While it works, it’s not very intuitive. It requires a lot of string concatenation and we have to be conscious of OS differences. For example, there is an os.sep attribute representing the path separator; that’s a / on POSIX-compliant OSes and \ for Windows. Using it can lead to code that looks like this:

Get hands-on with 1200+ tech skills courses.