Connect with us

Hi, what are you looking for?

Programming

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)

foundFile()

# Changing to the cwd (destination)
os.chdir("../")

foundFile()
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

os.mkdir(merge)
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)

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

Programming

This article is prepared to explain concepts that will be important for programming beginners such as data types, variables, heap and stack, and operators....

Programming

This article was created for those who want to use functional programming in their career or who are learning new programming paradigms. We will...

Programming

Functions are pieces of code that combine the integrity of the code used to perform a task into a single structure. If you are...

Programming

Computers can solve an event in different ways, we call these ways algorithms, each algorithm works differently from the others and has different processing...