fopen() and open() in cpython

eryk sun eryksun at gmail.com
Wed Aug 14 12:27:25 EDT 2019


On 8/13/19, Windson Yang <wiwindson at gmail.com> wrote:
> After my investigation, I found Since Python maintains its own buffer when
> read/write files, the build-in python open() function will call the open()
> system call instead of calling standard io fopen() for caching.  So when we
> read/write a file in Python, it would not call fopen(), fopen() only use
> for Python itself but not for python user. Am I correct?

Python 2 I/O wraps C FILE streams (i.e. fopen, fclose, fread, fwrite,
fgets). Python 3 has its own I/O stack (raw, buffered, text) that aims
to be more reliably cross-platform than C FILE streams. Python 3 still
uses FILE streams internally in some cases (e.g. to read pyvenv.cfg at
startup).

FYI in Windows open() or _wopen() is a C runtime library function, not
a system function. It calls the Windows API function CreateFile, which
calls the NT system function, NtCreateFile. It's similarly layered for
all calls, e.g. read() calls ReadFile or ReadConsoleW, which calls
NtReadFile or NtDeviceIoControlFile (ReadConsoleW).



More information about the Python-list mailing list