from _winapi import * import winfileio import io import os CREATE_NEW = 1 CREATE_ALWAYS = 2 OPEN_EXISTING = 3 OPEN_ALWAYS = 4 TRUNCATE_EXISTING = 5 h = CreateFile("hello.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL) with winfileio.WinFileIO(h, "w") as f: f.write(b"hello") f.write(b"world") x = f.tell() f.truncate(100); assert os.path.getsize("hello.txt") == 100 y = f.tell() assert x == y f.truncate(50) y = f.tell() assert x == y f.seek(0, io.SEEK_END) f.write(b"goodbye") h = CreateFile("hello.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL) with winfileio.WinFileIO(h, "r") as f: assert f.tell() == 0 print(f.read(20)) assert f.tell() == 20 f.seek(-15, io.SEEK_CUR) assert f.tell() == 5 print(f.read(5)) f.seek(0) print(f.read()) assert f.tell() == 50 + len(b"goodbye")