On this page
msvcrt
Prev: modulefinder | Up: stdlib_module | Next: multiprocessing
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — msvcrt
Tags: #public #python3 #stdlib
Import
import msvcrt
Overview
Windows-only interface to the Microsoft Visual C++ Runtime (MSVCRT) for low-level console keystrokes (kbhit(), getch(), getwch()), byte-range file locking (locking()), and CRT file-descriptor bridging (open_osfhandle(), get_osfhandle()). Use msvcrt when building interactive Windows terminal tools without curses or when locking files on Windows.
Quick Start
import sys
if sys.platform == "win32":
import msvcrt
# Non-blocking check for a keypress in a Windows console
if msvcrt.kbhit():
ch = msvcrt.getwch()
print("Pressed:", repr(ch))
Implementation Walkthrough
1. Windows File Locking (Counterpart to Unix fcntl)
Where Unix code uses fcntl, Windows scripts can lock byte ranges via msvcrt.locking(fd, mode, nbytes):
import sys
def lock_first_byte(fh) -> None:
if sys.platform == "win32":
import msvcrt
fh.seek(0)
msvcrt.locking(fh.fileno(), msvcrt.LK_NBLCK, 1)
Gotchas & Boundaries
- Windows-only: Raises
ModuleNotFoundErroron macOS and Linux; always guard withif sys.platform == "win32":. getch()returnsbytes,getwch()returnsstr: Prefergetwch()for Unicode input, and note that arrow/function keys emit a two-call prefix ('\x00'or'\xe0') followed by the scan code.- Cross-platform alternatives: Use
portalockerorfilelockfor portable file locking, andprompt_toolkitorrichfor portable terminal UIs.
Common Functions
- msvcrt.getch() —
getch() -> bytes— Read a keypress and return the resulting character as a byte string. - msvcrt.getwch() —
getwch() -> str— Wide char variant of getch(), returning a Unicode value. - msvcrt.kbhit() —
kbhit() -> bool— Return True if a keypress is waiting to be read. - msvcrt.locking() —
locking(fd, mode, nbytes)— Lock part of a file based on file descriptor fd from the C runtime.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/modulefinder
- tuts/python3/stdlib_module/msvcrt/getch
- tuts/python3/stdlib_module/msvcrt/getwch
- tuts/python3/stdlib_module/msvcrt/kbhit
- tuts/python3/stdlib_module/msvcrt/locking
- tuts/python3/stdlib_module/multiprocessing
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/modulefinder Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/msvcrt/getch Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/msvcrt/getwch Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/msvcrt/kbhit Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/msvcrt/locking Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/multiprocessing Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/argparse Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/curses Linked here; shared tags: #stdlib