Skip to main content
 首页 » 编程设计

python之pydoc.doc 无法检索到 "os.path.sep"

2024年12月31日9lvdongjie

我在交互模式下使用 os.path.sep 检查帮助实用程序,然后通过:

In [59]: help(os.path.sep) 
Related help topics: lambda, or, and, not, in, is, BOOLEAN, COMPARISON,BITWISE, SHIFTING, BINARY, FORMATTING, POWER, UNARY, ATTRIBUTES,SUBSCRIPTS, SLICINGS, CALLS, TUPLES, LISTS, DICTIONARIES 

Operator precedence


The following table summarizes the operator precedence in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for exponentiation, which groups from right to left).

但是,当我尝试使用 pydoc 检索它时

In [61]: with open("osPathSep_help.md", "w") as fout: 
    ...:     pydoc.doc(os.path.sep, output=fout) 
    ...:      
No Python documentation found for '/'. 
Use help() to get the interactive help utility. 
Use help(str) for help on the str class. 
 
In [62]: os.stat("osPathSep_help.md").st_size 
Out[62]: 0 

如何获取与报价相同的内容?

The following table summarizes the operator precedence in Python, from lowest precedence (least binding) to highest precedence (most

请您参考如下方法:

要了解此行为,您必须进入 os.path 库源代码。如果你深入代码,你会发现所有这些变量

curdir = '.' 
pardir = '..' 
extsep = '.' 
sep = '\\' 
pathsep = ';' 
altsep = '/' 
defpath = '.;C:\\bin' 
devnull = 'nul' 

现在您要做的是将 os.path.sep 作为函数访问,但如您所见,它不是一个函数,而是一个赋值为 '\'

所以 os.path.sep 总是 \ ,这就是为什么当你在做 help(os.path.sep) 你得到的结果是

Operator precedence

The following table summarizes the operator precedence in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for exponentiation, which groups from right to left).

这将与您将获得的结果相同

help('\\') 

现在您的下一个问题是它如何与 help 一起使用?

所以如果你查看帮助的源代码,就会提到它

""" 
Define the built-in 'help'. 
This is a wrapper around pydoc.help (with a twist). 
""" 

所以help是在内部使用pydoc.help

所以如果你正在使用

pydoc.help('\\') 或 pydoc.help(os.path.sep) 你会得到你想要的输出。