如何使用Python帮助文档查看文件函数
如果我想查看 str.replace()
这个函数的用法,可以用 help(str.replace)
,结果是:
Help on method_descriptor:
replace(...)
S.replace(old, new[, count]) -> str
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
(END)
但是我该怎么查看 file.read
或 readlines
的帮助呢?比如,使用 help(file.read)
和 help(read)
都会出错:
>>> help(file)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined
>>> help(file.read)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined
>>> help(read)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'read' is not defined
我该怎么用帮助功能查看文件相关的函数呢?
1 个回答
2
在Python 3中,file
这个类型已经被去掉了。你可以看看io
模块,它提供了相关的功能:
>>> import io
>>> help(io.TextIOBase.read)
Help on method_descriptor:
read(...)
Read at most n characters from stream.
Read from underlying buffer until we have n characters or we hit EOF.
If n is negative or omitted, read until EOF.