python opencv_Python open()

python opencv

Python open() function is used to open a file. This is the first step while working with files. Whether we want to read, write or edit files data, we first need to open it using open() function.

Python open()函数用于打开文件。 这是处理文件的第一步。 无论我们是要读取,写入还是编辑文件数据,我们首先需要使用open()函数将其打开。

Python open() (Python open())

Python open() function syntax is:

Python open()函数语法为:

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
  • file: specifies the file path object. Usually, a str or bytes object representing the file path. This is a mandatory argument.

    file :指定文件路径对象。 通常,一个str或bytes对象代表文件路径。 这是一个强制性的论点。
  • mode: specifies the file opening mode. There are different modes to open a file.
    • r: opens file in read-only mode.
    • w: opens the file in write mode, the file is truncated.
    • x: open for exclusive creation, failing if the file already exists
    • a: open for writing, appending to the end of the file if it exists
    • b: binary mode
    • t: text mode (default)
    • +: open a disk file for updating (reading and writing)

    File opened in binary mode return content of file as bytes without any decoding. Whereas files opened in text mode contents are returned as str, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding.

    mode :指定文件打开模式。 有多种打开文件的模式。
    • r :以只读模式打开文件。
    • w :以写入模式打开文件,文件被截断。
    • x :打开以进行独占创建,如果文件已经存在则失败
    • a :可以写入,如果存在则追加到文件末尾
    • b :二进制模式
    • t :文本模式(默认)
    • + :打开磁盘文件以进行更新(读取和写入)

    以二进制模式打开的文件以字节为单位返回文件内容,而没有任何解码。 以文本模式内容打开的文件以str返回,而首先使用平台相关的编码或使用指定的编码对字节进行了解码。

  • buffering: optional integer specifying the buffering policy. If passed as 0, buffering is turned off. This is allowed only when files are opened in binary mode. If passed as 1, line buffering is used and it’s allowed only in text mode. If passed greater than 1, then bytes of a fixed-size chunk buffer of specified size is used.

    buffering :指定缓冲策略的可选整数。 如果传递为0,则关闭缓冲。 仅当以二进制模式打开文件时才允许这样做。 如果以1传递,则使用行缓冲,并且仅在文本模式下才允许使用行缓冲。 如果传递的值大于1,则使用指定大小的固定大小的块缓冲区的字节。
  • encoding: name of the encoding used to decode or encode the file. It should be used only in text mode.

    encoding :用于解码或编码文件的编码名称。 仅应在文本模式下使用。
  • errors: an optional string that specifies how encoding and decoding errors are to be handled, this cannot be used in binary mode. Some of the standard values are strict, ignore, replace etc.

    errors :一个可选字符串,指定如何处理编码和解码错误,不能在二进制模式下使用。 一些标准值是严格,忽略,替换等。
  • newline: this parameter controls how universal newlines mode works (it only applies to text mode). It can be None, ”, ‘\n’, ‘\r’, and ‘\r\n’.

    newline :此参数控制通用换行模式的工作方式(仅适用于文本模式)。 可以是None,”,“ \ n”,“ \ r”和“ \ r \ n”。
  • opener: A custom opener can be used by passing a callable as opener.

    opener :可以通过传递一个可调用的opener来使用自定义的opener。

Most of the time, we use only file and mode parameters to open a file and perform necessary actions on it.

大多数时候,我们仅使用文件和模式参数来打开文件并对其执行必要的操作。

When a file is opened in text mode, TextIOWrapper instance is returned. When the file is opened in binary mode, BufferedRandom instance is returned.

在文本模式下打开文件时,将返回TextIOWrapper实例。 当以二进制模式打开文件时,返回BufferedRandom实例。

Python打开文件 (Python Open File)

Let’s look at some examples of opening file in python.

让我们看一些在python中打开文件的示例。

以文本和只读模式打开文件 (Open File in Text and Read Only Mode)

# open file in text and read only mode
f = open('data.txt', mode='r')

print(type(f))

f.close()

Output: <class '_io.TextIOWrapper'>

输出: <class '_io.TextIOWrapper'>

以二进制和只读模式打开文件 (Open File in Binary and Read Only Mode)

f = open('favicon.ico', mode='r+b')

print(type(f))

f.close()

Output: <class '_io.BufferedRandom'>

输出: <class '_io.BufferedRandom'>

以二进制模式打开文件,只读和缓冲 (Open file in binary mode, read only and buffer)

f = open('favicon.ico', mode='br', buffering=16)
f.close()

以二进制模式打开文件,只读且无缓冲 (Open file in binary mode, read-only and no buffering)

f = open('favicon.ico', mode='br', buffering=0)
f.close()

以文本模式打开文件,只读和行缓冲 (Open file in text mode, read-only and line buffering)

f = open('data.txt', mode='a', buffering=1)
f.close()

使用截断以写入模式打开文本文件 (Open text file in write mode with truncate)

f = open('data.txt', mode='w')
f.close()

以独占创建模式打开文件 (Open file in exclusive creation mode)

If the file already exists, passing ‘x’ as mode will throw FileExistsError. We can use try except block to catch this exception and perform corrective actions.

如果文件已经存在,则以“ x”作为模式传递将抛出FileExistsError。 我们可以使用tryexcept块来捕获此异常并执行纠正措施。

try:
    f = open('data.txt', mode='x')
except FileExistsError as e:
    print('file already exists')

Output: file already exists

输出: file already exists

That’s all for opening file in python, for more file related examples go through Python File Handling.

这就是在python中打开文件的全部内容,有关更多文件相关示例,请参见Python File Handling

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/22996/python-open

python opencv

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值