Python bindings for GNU parted library providing disk partition management capabilities
—
Python bindings for GNU parted library providing comprehensive disk partition management capabilities. pyparted offers both a high-level Pythonic interface and low-level direct access to libparted functionality, making it suitable for system administration tools, installers, and disk management applications.
pip install pypartedimport partedFor specific classes and functions:
from parted import Device, Disk, Partition, Geometry, Alignment, Constraint, FileSystemimport parted
# Get a device by path
device = parted.getDevice('/dev/sda')
print(f"Device: {device.model}, Size: {device.length} sectors")
# Read existing partition table
disk = parted.newDisk(device)
print(f"Disk type: {disk.type}")
# List partitions
for partition in disk.partitions:
print(f"Partition {partition.number}: {partition.getSize('GB'):.1f} GB")
# Create new partition table (CAUTION: destroys existing data)
# disk = parted.freshDisk(device, 'gpt')
# disk.commitToDevice()pyparted uses a dual-layer architecture:
Core classes follow a hierarchical design:
Device discovery, properties access, and low-level I/O operations for physical storage devices.
def getDevice(path: str) -> Device
def getAllDevices() -> list[Device]
def freeAllDevices() -> None
class Device:
# Properties
model: str
path: str
type: int
sectorSize: int
physicalSectorSize: int
length: int
busy: bool
readOnly: bool
# Methods
def open() -> None
def close() -> None
def clobber() -> None
def getConstraint() -> Constraint
def getMinimumAlignment() -> Alignment
def getOptimumAlignment() -> AlignmentReading, creating, and modifying partition tables including support for various formats like GPT, MBR, and other disk label types.
def newDisk(device: Device) -> Disk
def freshDisk(device: Device, ty: str) -> Disk
class Disk:
# Properties
device: Device
type: str
partitions: list[Partition]
primaryPartitionCount: int
maxPrimaryPartitionCount: int
# Methods
def commit() -> None
def commitToDevice() -> None
def commitToOS() -> None
def addPartition(partition: Partition, constraint: Constraint) -> None
def deletePartition(partition: Partition) -> None
def maximizePartition(partition: Partition, constraint: Constraint) -> NoneCreating, modifying, and managing individual partitions including setting flags, names, and filesystem types.
class Partition:
# Properties
disk: Disk
geometry: Geometry
fileSystem: FileSystem
number: int
path: str
type: int
name: str
active: bool
busy: bool
# Methods
def getFlag(flag: int) -> bool
def setFlag(flag: int, state: bool) -> None
def isFlagAvailable(flag: int) -> bool
def setSystem(fs: FileSystem) -> None
def setName(name: str) -> None
def getSize(unit: str = 'MB') -> floatSector-based geometry operations and alignment constraint management for precise partition positioning.
class Geometry:
# Properties
device: Device
start: int
end: int
length: int
# Methods
def intersect(geometry: Geometry) -> Geometry
def overlapsWith(geometry: Geometry) -> bool
def contains(geometry: Geometry) -> bool
def containsSector(sector: int) -> bool
class Alignment:
# Properties
offset: int
grainSize: int
# Methods
def alignUp(geometry: Geometry, sector: int) -> int
def alignDown(geometry: Geometry, sector: int) -> int
def isAligned(geometry: Geometry, sector: int) -> boolAdvanced constraint system for ensuring partition operations respect device limitations, alignment requirements, and size restrictions.
class Constraint:
# Properties
minSize: int
maxSize: int
# Methods
def intersect(constraint: Constraint) -> Constraint
def solveMax(geometry: Geometry) -> Geometry
def solveNearest(geometry: Geometry) -> Geometry
def isSolution(geometry: Geometry) -> boolFilesystem detection, probing, and type management for partition formatting and identification.
def probeFileSystem(geometry: Geometry) -> str
def probeForSpecificFileSystem(fstype: str, geometry: Geometry) -> Geometry
class FileSystem:
# Properties
type: str
geometry: Geometry
checked: boolHelper functions for unit conversion, version information, and system compatibility.
def formatBytes(bytes_: int, unit: str) -> float
def sizeToSectors(bytes_: int, unit: str, sector_size: int) -> int
def version() -> dict[str, str]
def getLabels(arch: str = None) -> set[str]pyparted provides extensive constants for device types, partition types, partition flags, disk flags, units, and exception handling.
Comprehensive exception system with specific exception types for different error conditions.
Install with Tessl CLI
npx tessl i tessl/pypi-pyparted