Python os module With Examples

Python OS module provides easy functions that allow us to interact and get Operating System information and even control processes up to a limit.

Python OS Module

The functions OS module provides allows us to operate on underlying Operating System tasks, irrespective of it being a Windows Platform, Macintosh or Linux. In this lesson, we will review these functions and what we can do with these.

Let us start our journey with these functions and what information they offer.

Python import os

Please note that first of all we have to import OS module in our program, then only we can execute any of it’s functions.


This function gives the name of the OS module it imports. This differs based on the underlying Operating System. Currently, it registers ‘posix’, ‘os2’, ‘ce’, ‘nt’, ‘riscos’ and ‘java’.

Let’s execute this on the system:

Clearly, this can output different platforms based on the interpreter.


environ is not a function but a process parameter through which we can access environment variables of the system.
Let’s see sample code snippet:

When we run this script, the following will be the output:


We can use it to work with the environment variables, read more at Python set environment variable.


execvp function is one of the ways to run other commands on the system.
Let’s see sample code snippet for this function:

For this, we just created a sample script as with following code:

When we run this script, the following will be the output:



This os module function returns current process’s user ID or UID, as it is populary known.

So, the current shell process ID is 501.


With the python os rename function, we can easily rename a file.

Note that for this, we must provide correct permissions to our script.


Python os system function allows us to run a command in the Python script, just like if I was running it in my shell. For example:

When I ran this script, a new file was made in the same directory with name users.txt and content String as ‘shubham’ as this is returned by original shell as well:


Note that this is a very powerful command and should be used cautiously.


Python os module error class is the base class for I/O related errors. So we can catch IO errors using OSError in the except clause.


This function returns current process ID or PID, as it is populary known.

So, the current shell process’s user ID is 71622.


This function just lists out the files and directories present in the current working directory.

It returns an iterable list of directory and file names.


This function return information which identifies current Operating System on which this is executed.

That was prettry detailed actually.

import os.path vs import os

os.path works strangely actually. It looks like os packaged with a submodule path but actually, os is a normal module which operate with sys.module to support os.path. Let us list what happens behind the scenes:

  • When Python starts, it loads many modules into sys.module.
  • os module is also loaded when Python starts. It assigns its path to the os specific module attribute.
  • It injects sys.modules['os.path'] = path so that you’re able to do import os.path as though it was a submodule.


In this lesson, we read about various functions provided by OS module in Python and saw how they work. See more lessons on Python here.

Reference: API Doc

By admin

Leave a Reply