如何使用python帮助文件功能

时间:2014-08-31 01:15:15

标签: python python-3.x

如果我想查看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.readreadlines? 例如,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 个答案:

答案 0 :(得分:2)

已从Python 3中删除file类型。请改为io module

>>> 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.