blkinfo
blkinfo is a Python package designed to retrieve and list detailed information about all available block devices on a system, or a specified block device. It provides a programmatic interface to access system-level block device data, typically parsing information found in `/sys/class/block`. The current version is 0.2.0, and releases are infrequent, reflecting its stable and specific functionality.
Common errors
-
FileNotFoundError: [Errno 2] No such file or directory: '/sys/class/block'
cause The `blkinfo` library is Linux-specific and depends on system paths like `/sys/class/block` which do not exist on other operating systems (e.g., Windows, macOS).fixRun your Python script in a Linux environment. If already on Linux, ensure the `/sys` filesystem is mounted correctly and accessible. -
PermissionError: [Errno 13] Permission denied: '/dev/sda'
cause The user executing the script lacks sufficient read permissions to access the specified block device or system files required by `blkinfo`.fixExecute your Python script with root privileges (e.g., `sudo python your_script.py`) or adjust the user's permissions to read from `/dev/` and `/sys/class/block`. -
ImportError: cannot import name 'BlkInfo' from 'blkinfo' (unknown location)
cause The `blkinfo` package is either not installed in the current Python environment, or there's a conflict with another module named `blkinfo`.fixInstall the package using `pip install blkinfo`. If it's already installed, ensure your current working directory doesn't contain a file named `blkinfo.py` that might be shadowing the installed package.
Warnings
- gotcha blkinfo is designed for Linux systems. Running it on other operating systems (e.g., Windows, macOS) will not yield correct results and may raise `FileNotFoundError` as it relies on Linux-specific paths like `/sys/class/block`.
- gotcha Accessing detailed block device information often requires root privileges. Running your Python script without `sudo` may result in partial or empty data for certain fields, or `PermissionError` when trying to access specific device paths.
- gotcha The JSON output schema for `get_all_blkdev_info()` and `get_blkdev_info()` received minor internal adjustments in version 0.2.0. While not a major API break, users who strictly parse the dictionary keys or rely on their exact ordering might need to adjust their code.
Install
-
pip install blkinfo
Imports
- BlkInfo
from blkinfo import BlkInfo
Quickstart
from blkinfo import BlkInfo
# Instantiate BlkInfo
blk_info = BlkInfo()
# Get information about all available block devices
all_block_devices_info = blk_info.get_all_blkdev_info()
print("\n--- All Block Devices ---")
for dev in all_block_devices_info:
print(f" Device: {dev['NAME']}, Size: {dev['SIZE']}, Type: {dev['TYPE']}")
# To get information about a specified block device (e.g., '/dev/sda')
# Note: Device paths vary by system. Use an existing path on your system.
# Permissions might be required (e.g., sudo) for full details.
import os
if os.path.exists('/dev/sda'):
try:
specified_block_device_info = blk_info.get_blkdev_info("/dev/sda")
print("\n--- Specific Block Device (/dev/sda) ---")
for key, value in specified_block_device_info.items():
print(f" {key}: {value}")
except Exception as e:
print(f"Could not get info for /dev/sda: {e}. (Often due to permissions or device not existing)")
else:
print("\n--- Specific Block Device (/dev/sda) ---")
print(" /dev/sda not found. Skipping specific device example.")
print(" Try with an existing path like '/dev/nvme0n1' or '/dev/vda' on your system.")