I'm trying to understand the expression structure of os.path.isdir
If type:
help(os)
I am provided with a list of 'things' python can use from os
I can't find an entry there called path
rather pathconf(...)
However, when I type:
help(os.path)
The options I am provided include isdir(s)
Why am I not seeing information about path
?
That's because the name path
is not defined directly in the module os.py
. Instead, it's imported from elsewhere and aliased. The implementation is platform-dependent.
On Windows systems you will have:
import ntpath as path
On Linux / macOS you will have:
import posixpath as path
Since os.path
is just a reference to another module such as posixpath
, or ntpath
, you can always look at help(os.path)
.
os.path
is a module. Therefore it does not appear in the help for os
. Rather it has its own help. os.path.__file__
will show you the actual path of this module. From the docs of os
:
- os.path is either posixpath or ntpath
Relevant part of the source of os
:
if 'posix' in _names:
# ...
import posixpath as path
elif 'nt' in _names:
# ...
import ntpath as path
os
is /usr/lib/python2.7/os.py
and os.path
is /usr/lib/python2.7/posixpath.py
(or ntpath.py
on Windows). The help
function simply reads the docstrings from those two files.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.