Python OS – Get location of root directory

In this post we will be creating a Python script that will retrieve the location of the users root directory. To do this we will be using the OS module which provides a portable way of using operating system dependent functionality.

The root directory is the first or top-most directory within a file system. The root directory may sometimes instead be referred to as the parent directory.

See the sample of Python code below where we retrieve the location of the root directory and return this to output.

import os

root_dir = os.path.abspath(os.sep)
print(f'Root directory: {root_dir}')

An example of output can be seen below.

Root directory: C:\

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply