Python OS Module














































Python OS Module



                      PYTHON OS MODULE

Python OS module provides the facility to establish the interaction between the user and the operating system. It offers many useful OS functions that are used to perform OS-based tasks and get related information about the operating system.
The OS comes under Python's standard utility modules. This module offers a portable way of using operating system dependent functionality.

Following are some functions in the OS module:-

os.name():-

This function gives the name of the operating system dependent module imported. The following names have currently been registered: posix, nt ,os2 , ce ,java, and riscos.

Example:-


Output:-



os.getcwd():-

Function os.getcwd(), returns the Current Working Directory(CWD) of the file used to execute the code, can vary from system to system.

Example:-


Output:-



os.error():-

All functions in this module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted by the operating system. os.error is an alias for built-in OSError exception.

Example:-


Output:-



os.popen():-

This function opens a file or from the command specified, and it returns a file object which is connected to a pipe.

Example:-


Output:-



os.close():-

Close file descriptor fd. A file opened using open(), can be closed by close()only. But file opened through os.popen(), can be closed with close() or os.close(). If we try closing a file opened with open(), using os.close(), Python would throw TypeError.

Example:-


Output:-



os.rename():-

A file old.txt can be renamed to new.txt, using the function os.rename(). The name of the file changes only if, the file exists and the user has sufficient privilege permission to change the file.

Example:-


Output:-



This is all about the Python os module.




Comments