t s
t s

Reputation: 171

python os library source code location

I'm in learning mode --- this is probably a dumb or rtfm question but here it is: Where is the source code for the Python os library located? I've downloaded the Python tarball. Lots of usages in there but can't seem to find the source code itself and how it hooks into the OS. I'm interested in seeing what's done for the os.* calls (e.g. os.read, os.write...). I've looked in *builtin* files but didn't see anything there. Thanks for the hints.

Upvotes: 17

Views: 11269

Answers (2)

nneonneo
nneonneo

Reputation: 179452

os's implementation is scattered across a number of files.

The core C functions are mostly located in Modules/posixmodule.c, which, despite its name, contains implementations for OS routines on Windows NT and OS/2, in addition to POSIX routines.

Wrappers for the C functions are located in Lib/os.py, and the os.path functions are provided by Lib/ntpath.py or Lib/posixpath.py depending on your platform.

Upvotes: 24

Ned Batchelder
Ned Batchelder

Reputation: 375634

If a module is defined in Python code, you can find the file by examining the __file__ attribute of the module after importing it:

>>> import os
>>> os.__file__
'C:\\python27\\lib\\os.pyc'

If the file is .pyc, then just drop the last 'c' to get the .py file.

But, the function you're looking for may actually be imported from a compiled C extension.

Upvotes: 6

Related Questions