Files
TransPyC/Test/SysLibTest/App/os_backend.py
2026-07-18 19:25:40 +08:00

40 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import t, c
from stdint import *
import platmacro
import os._win32
import os._posix
# =============================================================================
# Workaround: TransPyC 无法正确解析 os.xxx() 包级函数调用
# (os.path.xxx() 子模块调用正常,但 os.xxx() 包级调用解析为本地模块)
# 此文件单独导入 os._win32 / os._posix 子模块,避免污染调用方的 os.rmdir() 等解析。
# =============================================================================
def os_exists(path: str) -> bool:
"""检查文件/目录是否存在(跨平台)"""
if platmacro.IS_WINDOWS:
return os._win32.exists(path)
return os._posix.exists(path)
def os_remove(path: str) -> int:
"""删除文件(跨平台)。成功返回 0失败返回 -1。"""
if platmacro.IS_WINDOWS:
return os._win32.remove(path)
return os._posix.remove(path)
def os_isdir(path: str) -> bool:
"""检查路径是否为目录(跨平台)"""
if platmacro.IS_WINDOWS:
return os._win32.isdir(path)
return os._posix.isdir(path)
def os_getsize(path: str) -> t.CInt64T:
"""返回文件大小(字节,跨平台)。失败返回 -1。"""
if platmacro.IS_WINDOWS:
return os._win32.getsize(path)
return os._posix.getsize(path)