or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

constants-reference.mdconstraint-solving.mddevice-management.mddisk-operations.mdexception-handling.mdfilesystem-operations.mdgeometry-alignment.mdindex.mdpartition-management.mdutility-functions.md
tile.json

tessl/pypi-pyparted

Python bindings for GNU parted library providing disk partition management capabilities

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/pyparted@3.13.x

To install, run

npx @tessl/cli install tessl/pypi-pyparted@3.13.0

index.mddocs/

pyparted

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.

Package Information

  • Package Name: pyparted
  • Package Type: pypi
  • Language: Python
  • Installation: pip install pyparted

Core Imports

import parted

For specific classes and functions:

from parted import Device, Disk, Partition, Geometry, Alignment, Constraint, FileSystem

Basic Usage

import 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()

Architecture

pyparted uses a dual-layer architecture:

  • High-level parted module: Pythonic classes with advanced functionality
  • Low-level _ped module: Direct 1:1 mapping to libparted C functions

Core classes follow a hierarchical design:

  • Device: Physical storage device representation
  • Disk: Partition table container on a device
  • Partition: Individual partition within a disk
  • Geometry: Sector range definitions for regions
  • Alignment: Sector alignment constraints
  • Constraint: Complex operation restrictions
  • FileSystem: Filesystem type and properties

Capabilities

Device Management

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() -> Alignment

Device Management

Disk and Partition Table Operations

Reading, 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) -> None

Disk Operations

Partition Management

Creating, 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') -> float

Partition Management

Geometry and Alignment

Sector-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) -> bool

Geometry and Alignment

Constraint Solving

Advanced 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) -> bool

Constraint Solving

Filesystem Operations

Filesystem 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: bool

Filesystem Operations

Utility Functions

Helper 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]

Utility Functions

Constants and Enumerations

pyparted provides extensive constants for device types, partition types, partition flags, disk flags, units, and exception handling.

Constants Reference

Exception Handling

Comprehensive exception system with specific exception types for different error conditions.

Exception Handling