35.2. msvcrt – 来自 MS VC++ 运行时的有用例程

这些函数提供对 Windows 平台某些有用能力的访问。某些更高级模块使用这些函数来构建其服务的 Windows 实现。例如, getpass 模块使用这以实现 getpass() 函数。

可以在平台 API 文档编制中找到有关这些函数的进一步文档编制。

The module implements both the normal and wide char variants of the console I/O api. The normal API deals only with ASCII characters and is of limited use for internationalized applications. The wide char API should be used where ever possible

35.2.1. File Operations

msvcrt. locking ( fd , mode , nbytes )

锁定文件部分基于文件描述符 fd 从 C 运行时。引发 IOError 当失败时。文件的锁定区域从当前文件位置扩展 nbytes 字节,且在 EOF (文件末尾) 后可能继续。 mode 必须是某一 LK_* 常量下文有列出。可以同时锁定文件中的多个区域,但不可重叠。相邻区域不合并;必须单独解锁它们。

msvcrt. LK_LOCK
msvcrt. LK_RLCK

锁定指定字节。若无法锁定字节,程序在 1 秒后立即再试。若 10 次尝试后无法锁定字节, IOError 被引发。

msvcrt. LK_NBLCK
msvcrt. LK_NBRLCK

锁定指定字节。若无法锁定字节, IOError 被引发。

msvcrt. LK_UNLCK

解锁指定字节 (先前必须被锁定)。

msvcrt. setmode ( fd , flags )

设置行尾翻译模式为文件描述符 fd 。要把它设为文本模式, flags 应该为 os.O_TEXT ;对于二进制,它应该是 os.O_BINARY .

msvcrt. open_osfhandle ( handle , flags )

创建 C 运行时文件描述符从文件句柄 handle flags 参数应该是按位 OR 的 os.O_APPEND , os.O_RDONLY ,和 os.O_TEXT 。可以用作参数的文件描述符返回给 os.fdopen() 以创建文件对象。

msvcrt. get_osfhandle ( fd )

返回文件句柄对于文件描述符 fd 。引发 IOError if fd 无法识别。

35.2.2. Console I/O

msvcrt. kbhit ( )

返回 true 若按键正等待被读取。

msvcrt. getch ( )

Read a keypress and return the resulting character. Nothing is echoed to the console. This call will block if a keypress is not already available, but will not wait for Enter 被按下。若按下的键是特殊功能键,这会返回 '\000' or '\xe0' ; the next call will return the keycode. The Control-C 按键无法被读取采用此函数。

msvcrt. getwch ( )

宽字符变体的 getch() ,返回 Unicode 值。

2.6 版新增。

msvcrt. getche ( )

类似于 getch() ,但将回显按键,若它表示可打印字符。

msvcrt. getwche ( )

宽字符变体的 getche() ,返回 Unicode 值。

2.6 版新增。

msvcrt. putch ( char )

Print the character char 到控制台而不缓冲。

msvcrt. putwch ( unicode_char )

宽字符变体的 putch() ,接受 Unicode 值。

2.6 版新增。

msvcrt. ungetch ( char )

Cause the character char 被压回控制台缓冲;它将是下一个字符当读取通过 getch() or getche() .

msvcrt. ungetwch ( unicode_char )

宽字符变体的 ungetch() ,接受 Unicode 值。

2.6 版新增。

35.2.3. Other Functions

msvcrt. heapmin ( )

强制 malloc() 堆清理自身并将未使用块返回给操作系统。当故障时,这引发 IOError .