Connect with us

Hi, what are you looking for?

OS Module In Python Programming
OS Module In Python Programming


OS Module In Python Programming

In the previous article, we going to look at regular expression if you don’t read, you should read from here.

In this lesson we going to examine the OS module in python, also this tutorial is simple a module tutorial.

What Is Module And Why We Use?

You can think of modules as the main class, they are used to access functions, classes written by other programmers. You can create your own modules and use them in other projects.

We can provide many reasons to use modules, but the most important reason is to avoid the hassle of writing code over and over again. It is easier to use the code blocks you use regularly as modules

What Is OS Module And Why We Use?

OS (operating system) module gives us access to operating system features. We can do many features such as using console and managing files with the OS module.

In this tutorial, we will get to know the OS module and learn how to easily work with a module. Before learning the modules, it will be appropriate to learn the classes from here, you can start learning the classes.

Current Working Directory

The os module offers us an object named getcwd to show us in which environment we are working, this object shows which file we are working on.

import os

x = os.getcwd()

print("Currently Work:" , x)

Let’s develop a more advanced program to understand, let’s change the file location while the program is running and call getcwd again.

import os

def foundFile():
   x = os.getcwd()
   print("Currently Work:" , x)


# Changing to the cwd (destination)

Output: C:\Users\username\Desktop
Output: C:\Users\username

File Operations With OS Module

You can handle processes such as creating folders, reading directories content, deleting files with the OS module, let’s examine them one by one.

import os

file = "My Master Designer Folder"

# You must change this direction
direction = "C:\\Users\\username\\Desktop\\"

merge = file + direction

print("{} is created".format(file))
Output: My Master Designer Folder is created

You can use the listing method to see all the files in a certain directory. The OS module offers us an easy listing method.

import os

path = "C:\\Users\\username\\Desktop\\"

list = os.listdir(path)

Output: ["File1" , "File2" , "..."]

We can also use the OS module to delete files or folders, the remove and rmdir classes will help us.

import os

path = "C:\\Users\\username\\Desktop\\folder"
file = "name.txt"

os.rmdir(path) # Deleting folder
os.remove(file) # Deleting file

Console Management With OS Module

You can perform all console commands with the system class provided by the OS module. It is very simple to use, it is enough to write your command into the system.

import os

os.system("color a") # select green color

You can only enter one parameter, so use the format method for inputs that will be entered later. There is a very simple syntax, let’s examine it.

import os 

color_num = input("Color: ")

os.system("color ".format(color_num))
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like


clean code is essential for updating scripts, adding new features to code, etc. So every developer, must learn how to clean the code. With...

Daily News

Cloud computing is a system that is often heard today and that most developers use at least once. In this article, we’ll look at...

Game Development

Visual Studio Code without a doubt, most popular and loved code editor in the code editor market. Many developers love working with Vs Code....

Daily News

GitHub Copilot was introduced last year and continues to make it easier for developers with nice updates to date. Although most people see Github...