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.
You can find those in Modules/ , whereas the pure Python ones reside in Lib/ . In some cases (for example the json module), the Python source code provides the module on its own and only uses the C module if it's available (to improve performance).
The os. path module is a very extensively used module that is handy when processing files from different places in the system. It is used for different purposes such as for merging, normalizing and retrieving path names in python .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With