18

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 2

25

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.

Sign up to request clarification or add additional context in comments.

2 Comments

how to see the implementation of a particular function inside a module? I could find the path of os but I couldn't find the definition of getcwd() function.
@SilentMonk getcwd is one of the functions defined inside posixmodule.c. You can find its implementation here (Python 2.7.3): Modules/posixmodule.c#L1978
4

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

This is pretty much useless advice in the case of os. See neo's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.