The Basics of Python OS Module

Written by kalebujordan | Published 2020/10/17
Tech Story Tags: python | python-programming | python-tutorials | learn-python | python-tips | python-developers | python3 | web-monetization

TLDR The Basics of Python OS Module is a python module that provides the interface for interacting with the underlying operating system that Python is running. The 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 variety of situation in interacting with operating system. These methods range from creating new file & folders to renaming and deleting them. You can view all of them by using dir() as shown below.via the TL;DR App

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 variety of situation in interacting with the operating system ranging from creating new file & 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...........................
On this tutorial We will just check some of methods on OS Module

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'

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'

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'

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']
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']

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']

os.rename ( )

This 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']

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']

os.remove ( )

This OS Method is used to remove or delete a file path . For instance let’s remove os.py file
>>> import os
>>>os.listdir()
['documentation', 'os.py', 'os methods .png']
>>> os.rmdir('os methods .png')
>>> os.listdir()
['documentation', 'os.py']

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

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 environment variable HOME
>>> import os
>>> os.environ['HOME']
'/home/kalebu'
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()
1000

os.getpid ( )

Returns the real process ID of the current process.
>>> import os
>>> os.getpid()
19521

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 peer on Twitter( Tweet now ) and other dev communities.
In case of any suggestion, comment, or any difficulty drop it in the comment box below and I will get back to you ASAP.

Written by kalebujordan | I'm a Mechatronics engineer | Pro Python Developer | AI Enthusiast
Published by HackerNoon on 2020/10/17