Saturday, January 31, 2026
HomeLanguagesPython | os.major() method

Python | os.major() method

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

os.major() method in Python is used to extract the device major number from the specified raw device number (Usually the value of st_dev or st_rdev attribute of ‘os.stat_result’ object returned by os.stat() method). .

In Linux, everything is file, some are ordinary files while some are special files. Special files can be found under /dev directory. These special files are called device files. Character and block devices are the most common type of device files. These device files are represented as a pair of numbers ( major device number and minor device number) by Linux kernel.
Major device number indicates that which driver is used to access the hardware. Each driver in the system has a unique major number and all the device files whose major device number is same is controlled by the same driver.
While minor device number is used by the driver to distinguish between the various hardware it controls. Minor device number tells the kernel special characteristics of the device to be accessed.

Syntax: os.major(device)

Parameter:
device: An integer value representing the raw device number

Return Type: This method returns an integer value which represents the device major number.

Code: Use of os.major() method to extract the device major number from a raw device number




# Python program to explain os.major() method  
  
# importing os module 
import os
  
# Get the raw device number
# of a file
device = os.stat("file.txt").st_dev
  
# Print the raw device number
print("Raw device number:", device)
  
# Extract the device major number
# from the above raw device number
major = os.major(device)
  
# Print the device major number  
print("Device major number:", major)


Output:

Raw device number: 2056
Device major number: 8

Reference: https://docs.python.org/3/library/os.html#os.major

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
122 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12066 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS