Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python os library source code location

Tags:

python

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.

like image 345
t s Avatar asked Jan 30 '13 20:01

t s


People also ask

Where is Python library source code?

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).

What is the os path library in Python?

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 .


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.

like image 128
nneonneo Avatar answered Sep 29 '22 04:09

nneonneo


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.

like image 24
Ned Batchelder Avatar answered Sep 29 '22 03:09

Ned Batchelder