Introduction to the Python os module in a practical way, first part

The operating system is the main reason why your computer hardware is being powered. Without it, one would not be able to write and generate files in Microsoft Office, play games and navigate the internet with the help of a web browser.

Although a very complex software, the operating system functionalities can be easily accessed and used with the help of builtin modules offered by the Python computer programming language, such as the os module. Having experienced with the Python builtin os module, I have come to the conclusion that it deserves some attention through some specific articles about it, because it provides many useful utilities which the Python coder can utilize in their projects that deal with the operating system under the hood.

With the main purpose of keeping things as concrete as possible, I highly recommend that before you go any further with this article, you open a new Python interactive shell on your own operating system.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

The module which we are going to work with is the os module. Once you have launched your Python interactive console, make sure to import it by using the Python statement which is shown in the following example.

import os

The first function part of the Python builtin os module which I am going to explain is the getcwd(), a function which helps the Python coder to print the directory in which they’re currently working.

The Python geek can easily find out the directory which they’re currently working in by making use of the piece of code being shown below.

os.getcwd()

Once the above piece of Python code is being executed in the Python interactive console, a string will be displayed. A string which contains the absolute path of the directory in which you are currently running the Python interpreter.

After I executed the above command on my Python interactive console, I got the following output.

‘/Users/oltjano/Desktop/Coder/Python’

Not only can the Python coder make use of the builtin os module to find out the path of the directory in which they’re currently working in, but also to change the directory.

The function which can help the Python geek to change directory, is the one which is being shown below.

os.chdir

Available in Unix and Windows platforms, the above function needs a path as an argument.

The following practical example is a great one which illustrates the usage of the os.chdir utility.

os.chdir(‘/Users/oltjano/Desktop/Coder’)

The above Python command, changes the current working directory to the path being provided as the argument, which in the above example is /Users/oltjano/Desktop/Coder.

To make sure that the path has been changed, run the utility which checks the current working directory.

os.getcwd()
‘/Users/oltjano/Desktop/Coder’

As you can see from the above output, the path of my current working directory has been changed.

Another useful function which the Python builtin os module provides, is the one which finds out the user logged in on the controlling terminal of the process. The syntax for this function is the one which is being shown below.

os.getlogin()

Once the above piece of Python code has been executed on my interactive console, the following output is being displayed.

‘oltjano’

The above output informs the user that the one who is logged in on the controlling terminal of the process is ‘oltjano’.

Unfortunately for the Python geeks out there, the os.getlogin() functionality is available only in Unix based machines.

A builtin utility part of the os module which I like to make use of frequently in my Python based projects, is the listdir(). This utility is being used to list files inside a directory.

One can easily make use of the os.listdir utility by using it according to the syntax being shown below.

os.listdir(path)

For example, if one wants to list all their files inside a specific directory, all they need to do is provide the absolute path of that directory as an argument to the os.listdir utility.

my_path = ‘/Users/oltjano/Desktop/Coder’
os.listdir(my_path)

Once the above piece of Python code is being executed in my interactive shell, the following output gets displayed.

[‘Python’]

As you can see from the output being shown above, the os.listdir utility returns a list object with all the files inside the directory it checks.

As you can see from the practical examples we have been sharing so far, the Python os builtin module is very rich in features. It does not end in here. There are many functionalities provided by the Python os module which help the user to interact with their operating system through commands and scripts.

Another useful utility to the Python coder, especially to those who are passionate about system administration, is the os.mkdir. According to my personal experience with this utility, the coder can easily make use of it to create a new directory based on a custom path which should not exist.

The example shared below, illustrates how the Python coder can make use of the os.mkdir utility to create a new directory on their filesystem.

os.mkdir(‘/Users/oltjano/Desktop/Coder/Python/new_directory’)

To make sure if my new directory has been created or not, I make use of the os.listdir utility, a function which lists all the files inside a directory by returning them as elements of a Python list object.

os.listdir(‘/Users/oltjano/Desktop/Coder/Python’)
[‘new_directory’, ‘test.py’, ‘test.pyc’]

Based on the output being produced above, one can easily understand that my new directory called new_directory has been created.

One can easily remove a directory by using the builtin utility os.rmdir, part of the os module, like shown below.

os.rmdir(‘/Users/oltjano/Desktop/Coder/Python/new_directory’)

Once the above command has been executed in my Python interactive console, the directory gets removed. To make sure the directory is removed, I do a check with the os.listdir like shown in the example below.

os.listdir(‘/Users/oltjano/Desktop/Coder/Python’)
[‘test.py’, ‘test.pyc’]

Although the os.rmdir was executed successfully in the example being shown above, it does not work on directories which contains files. The os.rmdir utility works only on empty directories.

Let’s try and see with a practical example how os.rmdir fails to remove a directory which contains files.

os.rmdir(‘/Users/oltjano/Desktop/Coder’)

After the above command gets executed in my interactive Python console I get the following output.

Traceback (most recent call last):
File “”, line 1, in
OSError: [Errno 66] Directory not empty: ‘/Users/oltjano/Desktop/Coder’

As you can see from the above traceback, an OSError is being raised. It specifically says that the directory which the user is trying to remove with the help of os.rmdir is not an empty one.

According to the official Python documentation, another utility is being used to remove a directory which has files within it.

Final thoughts

The builtin Python os module provides various utilities and functions to the coder with the main purpose of helping them to interact with their operating system. In this first part, we took a look at some of the utilities provided by the os module, by providing practical examples for each one of them. Although most of the stuff is being clearly documented in the official documentation of the Python computer programming language, the beginner needs illustrations with practical examples to truly understand how theory can be manifested in real world scenarios.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *