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()
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.
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)
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.
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)
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
8. Check if there is a particular file present in your directory?
print(os.path.exists(“Name of the file”)
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.
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 services
We are well known for our interview books and have 70+ e-book across Amazon and The Data Monk e-shop page . Following are best-seller combo packs and services that we are providing as of now
- YouTube channel covering all the interview-related important topics in SQL, Python, MS Excel, Machine Learning Algorithm, Statistics, and Direct Interview Questions
Link – The Data Monk Youtube Channel - Website – ~2000 completed solved Interview questions in SQL, Python, ML, and Case Study
Link – The Data Monk website - E-book shop – We have 70+ e-books available on our website and 3 bundles covering 2000+ solved interview questions. Do check it out
Link – The Data E-shop Page - Instagram Page – It covers only Most asked Questions and concepts (100+ posts). We have 100+ most asked interview topics explained in simple terms
Link – The Data Monk Instagram page - Mock Interviews/Career Guidance/Mentorship/Resume Making
Book a slot on Top Mate
The Data Monk e-books
We know that each domain requires a different type of preparation, so we have divided our books in the same way:
1. 2200 Interview Questions to become Full Stack Analytics Professional – 2200 Most Asked Interview Questions
2.Data Scientist and Machine Learning Engineer -> 23 e-books covering all the ML Algorithms Interview Questions
3. 30 Days Analytics Course – Most Asked Interview Questions from 30 crucial topics
You can check out all the other e-books on our e-shop page – Do not miss it
For any information related to courses or e-books, please send an email to [email protected]