OS Module in Python

Hello everyone, In this tutorial, we’ll be learning about OS module in Python which provides flexibility to use Operating System dependent functionalities. It is an in-built module that comes with the Python installation. Let us begin this tutorial and learn some functions that are helpful to interact with the file-system.

Using OS Module functions in Python

Let us start by Importing the os module in our Program.

Import OS module in Python

import os

os.name in Python

  • os.name – This function returns the name of the operating system dependent module imported. Registered names are ‘posix’, ‘nt’, ‘os2’, ‘ce’, ‘java’ and ‘riscos’.
    • Windows XP has os.name of ‘nt’.
    • Mac OS X has os.name of ‘posix’.
a = os.name
print("os.name returns the name", a)
os.name returns the name nt

os.get_exec_path in Python

  • os.get_exec_path – This function returns the list of directories that will be searched for a named executable in your system. When we launch a program this list defines the order of the search for that program.
b = os.get_exec_path()
print("Directories to search name executables are : ",b)

Try to run the command and check the results.

os.getcwd in Python

  • os.getcwd – This function will return the path to the directory that is being used to run the current program that is the Current Working Directory(CWD). This path may vary from system to system.
c = os.getcwd()
print("current woring directory is: ",c)
D:\VS_code_workspace

os.mkdir in Python

  • os.mkdir – This function is used to create a directory in the specified path which is given as the argument and the name of the directory should be given in the path itself. After executing the code, we want to see whether the directory is created or not, for that, we are not going to open explorer and move to the path rather we use the next function.
d = os.mkdir(r'D:\VS_code_workspace\os_module')

os.listdir in Python

  • os.listdir – As the name suggests, this function will return a list of directories and files that are present in the path specified as the argument. If the path is not specified then it will take CWD by default.  Run the code and just look up the name of the directory.

Working with files using OS module

  • open(path, mode) – This function will open the file at the given path in the specified mode. If file is not present in the path then a new file is created automatically. Several modes for opening files are:
    • r – Read-only(default)
    • w – write-only
    • a – append mode
    • w+ – Reading and Writing
    • rb, wb, wb+ – Same as above but file will be opened in a Binary Format.

Note – We will use listdir to see whether the file is created or not and do remember to close the file if we are opening it.

newFile = open(r'D:\VS_code_workspace\os_module\my_first_file.txt','r')
print(os.listdir(r'D:\VS_code_workspace\os_module'))
['my_first_file.txt']

close() in OS module

  • close() – This function will close the file and clean up the memory allocated. This is necessary whenever we open a file. One way to open and close a file is by using the with keyword.
newFile.close()

os.rename() in OS module

  • os.rename(path1,path2) – This function can be used to rename a file as well as move a file. We have created a file name my_first_file.txt above having path ‘D:\VS_code_workspace\os_module\my_first_file.txt’, Now we will create a directory inside os_module and will move the file into that folder. See the code below and try to understand its working.

You may learn: Rename multiple files in python

os.mkdir(r'D:\VS_code_workspace\os_module\new_dir')
os.rename(r'D:\VS_code_workspace\os_module\my_first_file.txt', \
    r'D:\VS_code_workspace\os_module\new_dir\my_first_file.txt')
print(os.listdir(r'D:\VS_code_workspace\os_module\new_dir'))
['my_first_file.txt']

OS Module in Python

os.remove() – OS module

  • os.remove(path) – This function will delete the file specified in the path. We have a file in the new_dir directory which we will be removing and verify the operation by executing listdir function.

How to delete a file in Python with examples

os.remove(r'D:\VS_code_workspace\os_module\new_dir\my_first_file.txt')
print(os.listdir(r'D:\VS_code_workspace\os_module\new_dir'))
[]

We are getting an empty list because we only have a file in this directory and that too we have removed.

os.rmdir – OS module

  •  os.rmdir(path) – To remove a directory from the system we use this function. The directory should be empty otherwise, it will throw an error or no changes will be made.
os.rmdir(r'D:\VS_code_workspace\os_module\new_dir')

Hope you like this tutorial and if you have any doubt, feel free to comment below.

You may like to read:

Python File Handling

File Handling Using Pickle Module in Python with examples

Check whether a file/directory exists in Python

Leave a Reply

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