WinRT Windows.Storage.Streams

3.2.1 · active · verified Thu Apr 16

WinRT-Windows.Storage.Streams is a Python projection package providing access to the Windows Runtime (WinRT) APIs within the `Windows.Storage.Streams` namespace. This enables Python developers to interact with stream-related functionalities such as asynchronous read, write, and seek operations on various stream types. It is part of the modular PyWinRT project, which frequently releases updates synchronized with Windows SDK versions, with the current version being 3.2.1. [3, 7, 10]

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates writing bytes to an `InMemoryRandomAccessStream` using a `DataWriter`, then seeking to the beginning, reading the data back with a `DataReader`, and converting the `IBuffer` result into Python bytes. It showcases fundamental stream operations and the use of async methods.

import asyncio
from winrt.windows.storage.streams import DataWriter, DataReader, InMemoryRandomAccessStream, Buffer

async def write_and_read_stream():
    stream = InMemoryRandomAccessStream()
    writer = DataWriter(stream)

    # Write some bytes
    data_to_write = b"Hello, WinRT Streams!"
    writer.write_bytes(data_to_write)
    await writer.store_async()
    await writer.flush_async()

    # Seek to the beginning of the stream and read
    stream.seek(0)
    reader = DataReader(stream)
    await reader.load_async(stream.size)
    read_buffer = reader.read_buffer(stream.size)
    
    # Convert IBuffer to Python bytes using buffer protocol
    read_bytes = bytes(read_buffer) 
    
    print(f"Original: {data_to_write.decode('utf-8')}")
    print(f"Read:     {read_bytes.decode('utf-8')}")

    writer.close()
    reader.close()
    stream.close()

if __name__ == "__main__":
    asyncio.run(write_and_read_stream())

view raw JSON →