3

In my python standard library(Python 2.7), I am looking for specific methods within the modules.

For e.g. if i open re.py, i can see that there are specific methods like findall,search etc. The same is evident when i do dir(re). I can clearly see the above methods.

Similarly,when i do dir(os), there exists a method called system, which i usually call using os.system(cmd_name).

But when i look for this method under os.py, it does not exist. Am i doing something wrong? Please guide

1 Answer 1

5

This function is not defined in the os module, so you won't find the definition in os.py. It's imported into os from another module; which module that is will depend on your operating system. You can check:

>>> os.system.__module__
'nt'

Here, you can see that since I'm on Windows, os.system comes from the nt module.


You'll probably find that os.system.__module__ is either 'nt' or 'posix', representing either the nt or posix module. The question then is, where are those modules defined? There's no nt.py or posix.py.

It turns out these modules are implemented in C, in Modules/posixmodule.c. Yes, the nt module is posixmodule.c, and so is the posix module. It's weird.

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

6 Comments

How do i access nt module ? I want to see the code for def system
@fsociety: Answer expanded.
is there a way to look at the posixmodule.c ? Pardon my ignorance
@fsociety: If you look again, you'll see that I've provided a link!
@fsociety: You can't.
|

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.