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.

You May Also Like

Game Development

In this article, we will generate a movement script for FPS games in Unity. We’ll cover all the mechanics like camera control, crouching, and...

Game Development

In this article, we are going to look at how to use SQLite RDBMS in Unity 3D. After this article, you’ll be able to...

Machine Learning

In this article, we are going to learn how to build regression models with PyCaret in Python. If you don’t have any experience with...


In this article, we will make a simple login system in SwiftUI. After this article, you will be able to make a custom login...