简体   繁体   English

无法在python中获取有关os.path属性的信息

[英]Can't get info on os.path attribute in python

I'm trying to understand the expression structure of os.path.isdir 我试图了解os.path.isdir的表达结构

If type: 如果输入:

help(os)

I am provided with a list of 'things' python can use from os 我提供了python可以从os使用的``事物''列表

I can't find an entry there called path rather pathconf(...) 我在那里找不到名为path的条目,而是pathconf(...)

However, when I type: 但是,当我键入:

help(os.path)

The options I am provided include isdir(s) 我提供的选项包括isdir(s)

Why am I not seeing information about path ? 为什么我看不到有关path信息?

That's because the name path is not defined directly in the module os.py . 这是因为名称path未在os.py模块中直接定义。 Instead, it's imported from elsewhere and aliased. 相反,它是从其他位置导入并使用别名的。 The implementation is platform-dependent. 该实现取决于平台。

On Windows systems you will have: Windows系统上,您将具有:

import ntpath as path

On Linux / macOS you will have: Linux / macOS上,您将具有:

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只是对另一个模块(例如posixpathntpath ,因此您始终可以查看help(os.path)

os.path is a module. os.path是一个模块。 Therefore it does not appear in the help for os . 因此,它不会出现在os的帮助中。 Rather it has its own help. 相反,它有自己的帮助。 os.path.__file__ will show you the actual path of this module. os.path.__file__将向您显示此模块的实际路径。 From the docs of os : os的文档中:

  • os.path is either posixpath or ntpath os.path是posixpath或ntpath

Relevant part of the source of os : 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). os/usr/lib/python2.7/os.pyos.path/usr/lib/python2.7/posixpath.py (在Windows上是ntpath.py )。 The help function simply reads the docstrings from those two files. help功能仅从这两个文件中读取文档字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM