Python os.path.ismount() Method



The Python os.path.ismount() method is used to check whether a given path is a mount point in the file system. A mount point is a directory where a separate file system is mounted.

Suppose you have a separate partition or disk that you want to use for storing user data. You can mount that partition at a directory such as "/mnt/data". After mounting, any files and directories within "/mnt/data" are actually stored on the separate partition.

In the context of filesystems and operating systems, "mounted" refers to the process of attaching a filesystem to a specific directory (the mount point) within the filesystem hierarchy. When a filesystem is mounted, its contents become accessible through the directory where it is mounted.

Syntax

Following is the basic syntax of the Python os.path.ismount() method −

os.path.ismount(path)

Parameter

This method accepts a string as a parameter representing the path you want to check.

Return Value

The method returns a boolean value "True" or "False". It returns True if the specified path is a mount point, otherwise it returns False.

Example

In the following example, we are checking whether the root directory "/" is a mount point using the ismount() method −

import os
mount_path = "/"  
is_mount = os.path.ismount(mount_path)
print("The result obtained is:",is_mount) 

Output

The output obtained is as follows −

The result obtained is: True

Example

Here, we are checking if the directory "/home/lenovo/Documents" is a mount point or not −

import os
non_mount_path = "/home/lenovo/Documents"  
is_mount = os.path.ismount(non_mount_path)
print("The result obtained is:",is_mount) 

Output

Following is the output of the above code −

The result obtained is: False

Example

This example checks whether the path "/mnt" is a mount point. If "/mnt" is a mount point in your system, the result will be True, otherwise False −

import os
mount_path = "/mnt"  
is_mount = os.path.ismount(mount_path)
print("The result obtained is:",is_mount)   

Output

We get the output as shown below −

The result obtained is: True

Example

Now, we are checking if the non-existent file path "/non/existent/path" is a mount point or not −

import os
nonexistent_path = "/nonexistent/path"  
is_mount = os.path.ismount(nonexistent_path)
print("The result obtained is:",is_mount)  

Output

The result produced is as shown below −

The result obtained is: False
os_path_methods.htm
Advertisements