Python os.major() Method



The Python os.major() method extracts the device major number from a raw device number. This raw device number is obtained by either "st_dev" or "st_rdev" field of "os.stat_result" object returned by os.stat() method.

The os.stat() method is used to get the status of file descriptor.

NOTE: In Unix-like operating systems, every file is associated with a device number. This number is composed of two parts namely major and minor. The major number specifies the driver associated with the device, whereas the minor number is used by the driver to differentiate between different devices it controls.

Syntax

The syntax for os.major() method is shown below −

os.major(device)

Parameters

The Python os.major() method accepts a single parameter −

  • device − This is the raw device number (the st_dev or st_rdev attributes).

Return Value

The Python os.major() method returns the device major number.

Example

The following example illustrates the use of major() method. Here, we are retrieving the major device number from the value obtained by "st_dev" attribute.

import os, sys
path = "newFile.txt"

# Now get the touple
info = os.lstat(path)

# Get major device number
major_dnum = os.major(info.st_dev)
print ("Major Device Number:", major_dnum)

When we run above program, it produces following result −

Major Device Number: 8

Example

In this example, we are extracting the major device number of a file descriptor from the value obtained by "st_rdev" attribute.

import os
import stat

# path of terminal character device
deviceStat = os.stat("/home/shriansh/Python/tmp/new").st_rdev
mNumber = os.major(deviceStat)
print(f"The major device number for terminal character device: {mNumber}")

On executing the above program, it produces the following output −

The major device number for terminal character device: 0
python_files_io.htm
Advertisements