Python OS Module Tutorial with Examples

The python os module provides different operating systems related to functionalities. For example, if you want to list current operating system variables you can use the os.environ attribute which will print all operating system variables which may change according to the operating system type. The os module is named after the operating system where taking the first letters of it.

What Is OS Module In Python?

The Python OS module is a module to provide miscellaneous operating system methods and interfaces. The OS module is a built-in module that is provided by Python natively and there is no need to install an extra module. The Python OS module provides features about environment variables, path, user, process id, umask, file, random values, etc.

Is OS a Standard Python Library and Module?

The Python OS is a standard Python module this means there is no need to install extra modules or packages. It is provided by all recent and modern Python versions like Python 2.7 and Python 3.X.

How To Open/Import OS Module In Python?

As a standard Python module the os module can be easily opened or imported by using the import . Also specific method or submodule of the os module can be imported like below.

#Import OS module
import os

#Import All Methods and Attributes of the OS Module
import os.* – Print Operating System Name

The os module provides the name attribute via which will provide the name o the current operating system. The output will be POSIX where the current operating system is POSIX type which is generally used for Unix and Linux operating systems. There are also other operating system types like nt (For Windows operating systems), os2, ce, java and riscos.

import os


os.getcwd() – Print Current Working Directory or Path

While working with files, running commands there is a current working directory. The Current Working Directory or CWD can be printed with the getcwd() method like below.

import os


The current working directory will be the user home directory for Linux like /home/ismail and for Windows C:\Users\─░smail .

os.environ – List Environment Variables

Operating systems like Linux and Windows has environment variables in order to store some basic information. The os module provides the environ attribute in order to list environment variables.

import os

os.environ – List Environment Variables

os.chdir() – Change Current Working Directory

The chdir() method can be used to change the current working directory or path. The dhcidr() method accept the path as parameter where this can be an absolute or relative path.

import os

#Change Directory For Windows

#Change Directory For Linux

#Change Directory With Relative Path For Linux and Windows

os.mkdir() – Create Directory

The OS module provides the mkdir() method in order to create new directory. The directory name provided as parameter where the given name will be created in the current working directory. Also the absolute path and name of the directory can be used too.

import os

#Create Directory For Windows

#Create Directory For Linux

#Create Directory With Relative Path For Linux and Windows

os.listdir() – List All Files and Folders

The listdir() method is used to list all files and folders for the current working directory if no parameter is specified. Also a path can be provided the list the given path content file and folders.

import os

#Change Directory For Windows

#Change Directory For Linux

#Change Directory With Relative Path For Linux and Windows

os.error – Raise OSError Exceptions

Python is a modern programming language which also provides different type of exceptions. The OS module also provides OSError exceptions which are related with the invalid file names, paths or error to open files etc.

import os

   filename = "test.txt"
   f = open(filename,'r')
   text =

except IOError:
   print("The exception is" + file)

os.popen() – Run Operating System Command

The OS module provides the popen() method which will run an operating system command like executing it in the command line. This is called an open pipe where the command is executed and the output is written into the specified file.

import os

#This will run the ls command in read mode
output = os.popen("ls","r",1)

os.close() – Close File

The os module also provides the close() method which is used to close allready opened files.

import os

filename = "text.txt"

f = open(filename,'r')

content =



os.rename() – Rename File

The OS module also provides the rename() method which will simply rename the specified file into a new file name. But also the user has the required privileges in order to rename the given file into a new file name. We can provide the new and old file names as variable or string literal like below.

import os

filename = "test.txt"

os.rename(filename , "newtest.txt")

os.rename( "newtest.txt" , "morenewtest.txt")

os.remove() – Remove/Delete File

The os.remove() method is used to delete or remove the specified files. Keep in mind that this method can not delete directories or folders. The file can be specified as an absolute path or a relative path.

import os




Leave a Comment