-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.py
More file actions
40 lines (33 loc) · 1.21 KB
/
Copy pathapp.py
File metadata and controls
40 lines (33 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import asyncio
import tempfile
from pathlib import Path
from dynwinrt import RoApartment, projected_lifetime_scope
from generated.windows.storage import (
FileIO,
StorageFolder,
)
async def run() -> None:
expected = "Hello from dynwinrt.\nAsync WinRT file I/O works."
with tempfile.TemporaryDirectory(prefix="dynwinrt-python-") as directory:
with RoApartment(), projected_lifetime_scope():
folder = await StorageFolder.get_folder_from_path_async(directory)
file = await folder.create_file_async("sample.txt")
await FileIO.write_text_async(file, "Hello from dynwinrt.")
await FileIO.append_text_async(
file,
"\nAsync WinRT file I/O works.",
)
actual = await FileIO.read_text_async(file)
if actual != expected:
raise RuntimeError(
f"Unexpected file contents: {actual!r}"
)
print(
"python-file-io-ok",
{
"path": str(Path(directory) / "sample.txt"),
"characters": len(actual),
},
)
if __name__ == "__main__":
asyncio.run(run())