os module in python
The OS module in Python provides a way of using operating system dependent
functionality.
The functions that the OS module provides allows you to interface with the
underlying operating system that Python is running on – be that Windows, Mac or
Linux.
You can find important information about your location or about the process.
In this post I will show some of these functions.
# Python program to explain os.environ object
# importing os module
import os
# Get the value of
# 'HOME' environment variable
home = os.environ['HOME']
# Print the value of
# 'HOME' environment variable
print("HOME:", home)
# Get the value of
# 'JAVA_HOME' environment variable
# using get operation of dictionary
java_home = os.environ.get('JAVA_HOME')
# Print the value of
# 'JAVA_HOME' environment variable
print("JAVA_HOME:", java_home)
# Python program to explain os.rmdir() method
# importing os module
import os
# Directory name
directory = "ihritik"
# Parent Directory
parent = "/home/User/Documents"
# Path
path = os.path.join(parent, directory)
# Remove the Directory
# "ihritik"
os.rmdir(path)
print("Directory '%s' has been removed successfully" %directory)