Register Now


Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.


Register Now

It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways

OS module in Python – Complete Tutorial

We all have worked with Pandas and Numpy libraries, we might have also used matplot and some machine learning libraries. But, definitely os module in Python is one of the important modules present in the language. Once you recognize the importance of this module, then there is no going back.
OS module in Python is used to interact with your operating system.

When you working in a big data domain then you are bound to work on Hadoop ecosystem and if you working on something where you need to keep a tap on the way things are stored (whether Hadoop or your personal operating system) you need to understand the ways in which you can interact with the files, folders, and directories of the system. OS module helps you in the same.

In this blog, we will look into some important functions of the os module and rest you can practice on your own.

1. How to import the os module?

import os

2. How to get the current working directory?


OS module in Python

3. How to make a directory using this os module?

os.makedirs(“Name of the directory”)

Once you execute this code, you will have a new folder in your current working directory.
os.makedirs() method in Python is used to create a directory recursively. That means while making leaf directory if any intermediate-level directory is missing, os.makedirs() method will create them all.

OS module in Python

4. How to get the list of all the files in your root directory using os module?

Use the power of listdir()

Example :-

x = “The location from where you need to list the directories”
dir_list = os.listdir(x)

OS module in Python

5. How to remove a directory using os module?


os.remove() method in Python is used to remove or delete a file path. This method can not remove or delete a directory. If the specified path is a directory then OSError will be raised by the method.
os.rmdir() method in Python is used to remove or delete an empty directory. OSError will be raised if the specified path is not an empty directory.

OS module in Python

6. You have a file name in one list or variable and path in another, how will you get to the file?

x = “Alpha”
path = “Users/kamal/Documents/prod”
final_path = os.path.join(path,x)

OS module in Python

7. You have to extract all the file with extension .sql names present in a particular folder, how to do this?

The problem statement is to create a list of all the sql files present in any directory or folder in your root folder.
Here we will be using the os.walk()

We declared a list with the name sql_file, then in the mypath variable we have the complete path of the directory in which you have to find the .sql file
Now walk() is used when you have to parse in multi-level folders.
In each of the variable the folder,path and file contains the name of the directory, path of the file and file name. Since we need to check the file name which ends with .sql, so we will parse only the file. The if and append conditions are intuitive

OS module in Python

8. Check if there is a particular file present in your directory?

print(os.path.exists(“Name of the file”)

OS module in Python

9. How to split the file name and path of the file using os module?

use the function os.path.split(“Complete path”)
This method splits the pathname into a pair of head and tail. Here, the tail is the last pathname component and the head is everything that comes before it. The method returns a tuple of the head and tail of the specified path.

OS module in Python

10. How to get the time of the last modification of a file?

os.path.getmtime() :
This method returns the time of the last modification of the path

These examples are just to show the capability of os module in Python, a lot of problems can be solved if you know how to use this module.

The Data Monk Interview Books – Don’t Miss

Now we are also available on our website where you can directly download the PDF of the topic you are interested in. At Amazon, each book costs ~299, on our website we have put it at a 60-80% discount. There are ~4000 solved interview questions prepared for you.

10 e-book bundle with 1400 interview questions spread across SQL, Python, Statistics, Case Studies, and Machine Learning Algorithms – Ideal for 0-3 years experienced candidates

23 E-book with ~2000 interview questions spread across AWS, SQL, Python, 10+ ML algorithms, MS Excel, and Case Studies – Complete Package for someone between 0 to 8 years of experience (The above 10 e-book bundle has a completely different set of e-books)

12 E-books for 12 Machine Learning algorithms with 1000+ interview questions – For those candidates who want to include any Machine Learning Algorithm in their resume and to learn/revise the important concepts. These 12 e-books are a part of the 23 e-book package

Individual 50+ e-books on separate topics

Important Resources to crack interviews (Mostly Free)

There are a few things which might be very useful for your preparation

The Data Monk Youtube channel – Here you will get only those videos that are asked in interviews for Data Analysts, Data Scientists, Machine Learning Engineers, Business Intelligence Engineers, Analytics Manager, etc.
Go through the watchlist which makes you uncomfortable:-

All the list of 200 videos
Complete Python Playlist for Data Science
Company-wise Data Science Interview Questions – Must Watch
All important Machine Learning Algorithm with code in Python
Complete Python Numpy Playlist
Complete Python Pandas Playlist
SQL Complete Playlist
Case Study and Guesstimates Complete Playlist
Complete Playlist of Statistics

About TheDataMonkGrand Master

I am the Co-Founder of The Data Monk. I have a total of 6+ years of analytics experience 3+ years at Mu Sigma 2 years at OYO 1 year and counting at The Data Monk I am an active trader and a logically sarcastic idiot :)

Follow Me