DEV Community

Cover image for Learn the basics of python OS module
Jordan Kalebu
Jordan Kalebu

Posted on • Updated on

Learn the 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 
>>>dir(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...........................
Enter fullscreen mode Exit fullscreen mode

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
'/home/kalebu'
Enter fullscreen mode Exit fullscreen mode

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
'/home/kalebu'
>>>os.chdir('Desktop') 
>>>os.getcwd() #getting path after changing dir
'/home/kalebu/Desktop'
Enter fullscreen mode Exit fullscreen mode

os.listdir ( )

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

>>>import os 
>>>all_dirs = os.listdir()
>>>print(all_dirs)
['documentation', 'app.py', 'os methods .png']
Enter fullscreen mode Exit fullscreen mode

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)
['learn.py', 'requirements.txt', 'you.py']
Enter fullscreen mode Exit fullscreen mode

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','app.py', 'os methods .png']
>>>os.mkdir('Products') # os.mkdir(directory_name)
>>>os.listdir() #after making a new dir
['documentation', 'Products', 'app.py', 'os methods .png']
Enter fullscreen mode Exit fullscreen mode

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 app.py to os.py.

>>> import os
>>>os.listdir()
['documentation', 'Products', 'app.py', 'os methods .png']
>>> os.rename('app.py', 'os.py') # os.rename('oldname', 'newname')
>>> os.listdir()
['documentation', 'Products', 'os.py', 'os methods .png']
Enter fullscreen mode Exit fullscreen mode

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
>>>os.listdir()
['documentation', 'Products', 'app.py', 'os methods .png']
>>> os.rmdir('Products')
>>> os.listdir()
['documentation', 'os.py', 'os methods .png']
Enter fullscreen mode Exit fullscreen mode

os.remove ( )

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

>>> import os
>>>os.listdir()
['documentation', 'os.py', 'os methods .png']
>>> os.rmdir('os methods .png')
>>> os.listdir()
['documentation', 'os.py']
Enter fullscreen mode Exit fullscreen mode

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
   ├── learn.py
   ├── requirements.txt
   └── you.py
└── os.py

1 directory, 4 files
0
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

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']
'/home/kalebu'
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

os.getuid ( )

This Module returns the current process’s user id.

>>> import os
>>> os.getuid()
1000
Enter fullscreen mode Exit fullscreen mode

os.getpid ( )

Returns the real process ID of the current process.

>>> import os
>>> os.getpid()
19521
Enter fullscreen mode Exit fullscreen mode

Summary

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.

Hope you find it interesting, now don’t be shy share it with your fellow developers.

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

The original article can be found on kalebujordan.dev

Top comments (1)

Collapse
 
byoussef profile image
B. Youssef

Informative article, thank you 👍🏼