Python OS Modules Introduction

aimtocode

OS Module Definition

The os module provides functions for working with files and directories (“os” stands for “operating system”). os.getcwd returns the name of the current directory:

This module provides a portable way of using operating system dependent functionality.


Example: os.environ

os.environ module is used to process parameter through which we can access environment variables of our system.

 import os
 output = os.environ['HOME']
 print(output)

Output:

 C:\Users\Python

Example: os.rename

os.rename function allows programmer to rename the file name.

 import os
 fileDir = "pythonmodule.txt"
 os.rename(fd,'RenamedName.txt')

Example: os.name

os.name function tells the name of the operating system module that it imports. Currently, it registers 'posix', 'nt', 'os2', 'ce', 'java' and 'riscos'.

 import os   
 print(os.name)   

Output:

 nt

Example: os.getcwd()

os.getcwd() function returns the Current Working Directory(CWD) of the file.

 import os   
 print(os.getcwd())

Output:

 C:\Users\Python\Python37-32	
aimtocode