Latest Code Tutorials

Python OS Module Tutorial With Example


Python OS Module Tutorial With Example is today’s topic.  The OS module in Python provides a way of using operating system dependent functionality. The functions that the OS module provides allows you to interface with the underlying operating system that Python is running on – be that Windows, Mac or Linux. The functions OS module provides us for operating on underlying Operating System tasks, irrespective of it being a Windows Platform, Macintosh or Linux.

Python OS Module Tutorial

The os module is the part of the standard library, or stdlib, within Python 3. This means that it comes with your Python installation, but you still must import it. Write the following code to import the OS module. If you do not know what is a module in Python, then you can check out this Python Module article.

import os

Now, let’s see some of the essential functions of os in detail.

The function gives the name of the OS module it imports. This differs based on the underlying Operating System. 


import os


See the output below.


Python OS Module Tutorial


The environ is not a function but a process parameter through which we can access environment variables of the system. Let’s see the following example.

import os


See the output.


Python OS environ

We can also print the HOME environment.


import os


See the following output.


Python OS environment example


The getcwd function of OS module will give us the current directory of the project.


import os


See the following output.


Python OS getcwd example

If you want to make a new directory type the following code.


import os


It will create a new directory called newDir inside the current folder.


The execvp function is one of the ways to run other commands on the system. Let’s see the following example.

Create one file inside the same folder called and add the following code.


student = {
  'name': 'Krunal',
  'enno': 21,
  'college': 'vvp college'

Now, write the following code inside the file.


import os
program = 'python'
arguments = ['']
print(os.execvp(program, (program,) + tuple(arguments)))

Now, run the file.


Python os execvp example


It returns the real group id of the current process.


import os


It returns the 20. That means the group id of the current process is 20.


The os.getuid os module function returns the current process’s user ID or UID, as it is popularly known.

See the following example.

import os


It returns the 501.


The os.getpid returns the process ID of the current process.


import os



Python os getpid example


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


import os

newFile = os.system('users > app.txt')

If you run the above file, the new file is created called app.txt and inside that krunal is written because I am the user of my computer. You can see your name.

There are so many other OS modules that you can use in your project as per your requirement.

Finally, Python OS Module Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.