What is Python's OS Module

PythonServer Side ProgrammingProgramming

Introduction

The OS module in Python comes with various functions that enables developers to interact with the Operating system that they are currently working on. In this article we’ll be learning mainly to create and delete a directory/folder, rename a directory and even basics of file handling.

Without further ado, let’s get started.

Getting Started

Python’s OS module comes packaged within python when installed. This means you do not need to separately install it using PIP. In order to access its various methods/functions, you just need to import the module.

import os

Now that you’ve imported the module, you can start using its various functions.

Getting current working directory

The currently working directory is the folder in which the python script is saved and being run from.

import os
os.getcwd()

Note − Directory is nothing but folder.

Creating a directory

import os
os.mkdir("D:\\Tutorialspoint")

This will create a folder Tutorialspoint in the D drive.

Note − If no location is specified, it will just create a “new folder” in the current working directory.

Deleting a directory

Now that you know how to create a folder, let us learn about how you can delete one.

In order to delete a directory, we will be using the rmdir() function, it stands for remove directory.

import os
os.rmdir("D:\\Tutorialspoint")

Note: While deleting a directory/folder is possible using Python, you cannot delete the current working directory −> The folder in which the python script is being executed from.

Renaming a directory

In order to rename a folder, we use the rename function present in the os module.

import os
os.mkdir("D:\\Tutorialspoint")
os.rename("D:\\Tutorialspoint","D:\\Tutorialspoint2")

The above line of code renames Tutorialspoint to Tutorialspoint2.

Basic file manipulation

Now that you know how to work around with folders, let us look into file manipulation.

Creating a file

file = os.popen("Hello.txt", 'w')

A file named Hello.txt is created in the current working directory.

Adding content to the created file

file = os.popen("Hello.txt", 'w')
file.write("Hello there! This is a tutorialspoint article")

Note − You can use os.rename to rename files as well. Just make sure you get their extensions right.

Example

Given below is the complete program to test out all the above-mentioned scenarios:

import os
os.getcwd()
os.mkdir("D:\\Tutorialspoint")
os.rmdir("D:\\Tutorialspoint")
os.mkdir("D:\\Tutorialspoint")
os.rename("D:\\Tutorialspoint","D:\\Tutorialspoint2")
file = os.popen("Hello.txt", 'w')
file.write("Hello there! This is a tutorialspoint article")

Conclusion

You now know the basic functions of OS module and how or where we use it during python development.

There are a lot more versatile functions within the OS module, to explore further, do read from their official documentation at https://docs.python.org/3/library/os.html.

raja
Published on 11-Feb-2021 06:47:20
Advertisements