OS Module in Python:

This module provides functions for interaction with operating system. The OS module comes under the Python’s standard utility modules used for Data Science. The module provides a portable way of how to use operating system dependent functionality. The modules *os* and *os.path* includes many functions in order to interact with the file system.

Below we will discuss some functions in the OS module:

  1. os.name: When we use this function, it gives name of the operating system dependent module. The registered names are as follows: ‘posix’, ‘nt’, ‘os2’, ‘ce’, ‘java’, and ‘riscos’.
  2. os.getcwd(): The function os.getcwd() when called returns the current working directory(CWD) of file in use for executing the code. It can differ from system to system.OS Module in Python for Data Science - PST Analytics

In GFG interpreter, we use directory \root.

  1. os.error: The functions in this module raise OSError in case of invalid or inaccessible file names and paths. This also happens in case of arguments having correct type but not accept the operating system. os.error is the alias for built-in OSError exception.

OS Module in Python for Data Science - PST Analytics

File Object Manipulation:
  1. os.popen(): This is a method which opens a pipe to or from command. The value which is return can be return or written, which depends on whether the mode is ‘r’ or ‘w’.


The parameters mode and bufsize are not necessary parameters. In case it is not provide the mode is taken as ‘r’.

OS Module in Python for Data Science - PST Analytics

The output for popen() is not shown; the changes will be reflected directly into the file.

  1. os.close(): When we open a file using open() can only be closed using close(). But in case we open a file through os.popen(), it can only be closed using close() or os.close(). In case we try to close a function which is opened by open() by os.close(), Python will throw TypeError.

In case there is a non-existent file or permission privilege, we won’t get the same error.

  1. os.rename(): We can use this function in order to rename a file old.txt to new.txt. The file name changes only in case the file exists, and the user has sufficient privilege permission to change the file.

OS Module in Python for Data Science - PST Analytics

To see more about OS module in python for data science, you can check this and this as well.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.