Register Now

Login

Lost Password

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

Login

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.getcwd()

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)
print(list_dir)

OS module in Python

5. How to remove a directory using os module?

os.remove()
os.rmdir()

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)
print(final_path)

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.

Some Useful resources for SQL and Python interview questions

Flipkart Interview Question – How to get all the rows with NULLs from multiple columns – Solved with 3 different ways
How SQL Query is executed at the back end
Understand the concept of cumulative sum in SQL – One of the most asked concepts

Exploding a column in SQL(Unnesting a column)

Myntra SQL interview question (Very important concept) – https://www.youtube.com/watch?v=wor2osrIjzc

Most asked SQL concept asked in almost all the product based companies interview – https://www.youtube.com/watch?v=aqpkrJvvqxs&t=4s

If you are wondering how to make a career in the Analytics domain, then our 10 e-book bundle with 1400+ direct interview questions are for you Check out our booklist



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

Leave a reply