gpt4 book ai didi

python - 是否有文件对象的文档?

转载 作者:行者123 更新时间:2023-12-03 13:54:46 24 4
gpt4 key购买 nike

这可能是一个非常愚蠢的问题,但老实说,我在 Python 3 中找不到文件对象 API 的文档。

使用或返回文件对象的 Python 文档,例如 opensys.stdin有链接到词汇表的高级介绍。它没有列出这些对象公开的函数,我不知道,我可以用它们做什么。我试过谷歌搜索文件对象文档,但搜索引擎似乎不明白,我在寻找什么。

我是 Python 的新手,但不是一般的编程。到目前为止,我使用对象的方案是找到完整的 API 引用,看看它可以做什么,然后选择要在我的代码中使用的方法。这是 Python 世界中的错误心态吗?有哪些替代方案?

最佳答案

open返回 file object因模式而异。来自 open文档:

The type of file object returned by the open() function depends on the mode. When open() is used to open a file in a text mode ('w', 'r', 'wt', 'rt', etc.), it returns a subclass of io.TextIOBase (specifically io.TextIOWrapper). When used to open a file in a binary mode with buffering, the returned class is a subclass of io.BufferedIOBase. The exact class varies: in read binary mode, it returns an io.BufferedReader; in write binary and append binary modes, it returns an io.BufferedWriter, and in read/write mode, it returns an io.BufferedRandom. When buffering is disabled, the raw stream, a subclass of io.RawIOBase, io.FileIO, is returned.



由于它有所不同,请使用您需要帮助的模式打开文件对象并寻求帮助:
>>> f = open('xx','w')
>>> help(f)

Help on TextIOWrapper object:

class TextIOWrapper(_TextIOBase)
| Character and line based layer over a BufferedIOBase object, buffer.
|
: etc...

关于python - 是否有文件对象的文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49887909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com