Basics of python OS Module
3 min read

Basics of python OS Module

Basics of python OS Module

The OS module is a python module that provides the interface for interacting with the underlying operating system that Python is running.

This module provides a portable way of using operating system dependent functionality.

OS module comes by default on Python’s standard utility modules therefore you don’t need to install anything to start using it.

OS module provides a ton of methods of which you use in a variety of situations in interacting with the operating system ranging from creating new files & folders to renaming and deleting them.

You can view all of them by using dir() as shown below

>>>import os 
'chown', 'chroot', 'close', 'closerange', 'confstr', 'confstr_names', 
'cpu_count', 'ctermid', 'curdir', 'defpath', 'device_encoding', 
'posix_fallocate', 'pread', 'putenv', 'pwrite', 'read', 'readlink', 
'readv', 'remove', 'removedirs', 'removexattr', 'rename', 'renames'
............................and so on...........................

On this tutorial We will just check some of the methods on OS Module that are common; let's get started

os.getcwd ( )

This OS method is used to get the path of the current working directory.

>>>import os 
>>>os.getcwd() #getting path of current directory

os.chdir ( )

This OS method is used to change the current directory into a new directory, it receives one parameter which is the path of the new directory.

>>>import os 
>>>os.getcwd() #getting path before changing dir
>>>os.getcwd() #getting path after changing dir

os.listdir ( )

This OS method returns all files and sub-directory on the current directory.

>>>import os 
>>>all_dirs = os.listdir()
['documentation', '', 'os methods .png']

Also, you can pass a parameter of a specific directory to view its file and sub-directory

>>> import os
>>> doc_dirs = os.listdir('documentation')
>>> print(doc_dirs)
['', 'requirements.txt', '']

os.mkdir ( )

This OS method is used to create a new directory of the current path, for instance, let’s create a directory named Products.

>>>import os 
>>>os.listdir() #before any change
['documentation','', 'os methods .png']
>>>os.mkdir('Products') # os.mkdir(directory_name)
>>>os.listdir() #after making a new dir
['documentation', 'Products', '', 'os methods .png']

os.rename ( )

OS Module is used to rename a file or folder, it receives two-argument, an old name and a new name of file or directory. For instance, let’s rename to

>>> import os
['documentation', 'Products', '', 'os methods .png']
>>> os.rename('', '') # os.rename('oldname', 'newname')
>>> os.listdir()
['documentation', 'Products', '', 'os methods .png']

os.rmdir ( )

This OS method is used to remove a directory it receives an argument of the name of the directory you want to remove. os.rmdir() will only work for empty dirs

for instance, Let’s remove the Products directory using rmdir().

>>> import os
['documentation', 'Products', '', 'os methods .png']
>>> os.rmdir('Products')
>>> os.listdir()
['documentation', '', 'os methods .png']

os.remove ( )

This OS Method is used to remove or delete a file path. For instance, let’s remove the file.

>>> import os
['documentation', '', 'os methods .png']
>>> os.rmdir('os methods .png')
>>> os.listdir()
['documentation', '']

os.sytem ( )

This OS Method is used to run a shell command on the python program as if you were in shell. For instance, let’s run a tree command using the system method.

>>>import os 
>>> os.system('tree')
├── documentation
│   ├──
│   ├── requirements.txt
│   └──
1 directory, 4 files

os.uname ( )

This OS Method returns information identifying the current operating system.

>>>import os 
>>> os.uname()
posix.uname_result(sysname='Linux', nodename='kalebu-PC', release='4.15.0-30deepin-generic', version='#31 SMP Fri Nov 30 04:29:02 UTC 2018', machine='x86_64')

os.environ []

environ is not a method in the OS module rather than it’s a process parameter through which we can access the environment variables of the system.

For instance, Let’s access the environment variable HOME

>>> import os
>>> os.environ['HOME']

We also set our own environment variable as shown below

>>> import os
>>> os.environ['my_secret_key'] = "Life sucks" #setting our own variable
>>> os.environ['my_secret_key'] #accesing it 
'Life sucks'

os.getuid ( )

This Module returns the current process’s user id.

>>> import os
>>> os.getuid()

os.getpid ( )

Returns the real process ID of the current process.

>>> import os
>>> os.getpid()


In this tutorial we learned about OS Module and how can you use it in a variety of operating system related tasks using its methods.

In case of any suggestion, comment, or any difficulty drop it in the comment box below and I will get back to you ASAP.