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.
2 Answers
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.
2 Comments
os but I couldn't find the definition of getcwd() function.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.
1 Comment
os. See neo's answer.