Python OS Module: How to Use OS Module In Python 3

Python OS module provides a way of using operating system dependent functionality. The functions that the OS module provides allow you to interface with the underlying operating system that Python is running on: be that Windows, Mac or Linux.

If you just want to read or write a file then use the open() function, if you want to manipulate paths, then use the os.path module, and if you want to read all the lines in all the files on the command line then use the fileinput module. For creating temporary files and directories, use the tempfile module, and for high-level file and directory, handling sees the shutil module.

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 Example

The OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. The os module provides a portable way of using operating system dependent functionality. The *os* and *os.path* modules include many functions to interact with the file system.

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.

Python OS Module Tutorial


The environ is not a function but a process parameter through which we can access the 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 output.

Python OS environment example


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


import os


See the 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 as 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.


All functions in this module raise OSError in the event of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not trusted by the operating system. Python os.error is an alias for built-in OSError exception.

import os
    filename = 'omg.txt'
    f = open(filename, 'rU')
    text =

except IOError:
    print('Problem reading: ' + filename)


➜  pyt python3
Problem reading: omg.txt
➜  pyt

Finally, Python OS Module Example is over.

Leave a Comment

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