英文文档:

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

Open file and return a corresponding file object. If the file cannot be opened, an OSError is raised.

file is either a string or bytes object giving the pathname (absolute or relative to the current working directory) of the file to be opened or an integer file descriptor of the file to be wrapped. (If a file descriptor is given, it is closed when the returned I/O object is closed, unless closefd is set to False.)

mode is an optional string that specifies the mode in which the file is opened. It defaults to 'r' which means open for reading in text mode. Other common values are 'w' for writing (truncating the file if it already exists), 'x' for exclusive creation and 'a' for appending (which on some Unix systems, means that all writes append to the end of the file regardless of the current seek position). In text mode, if encoding is not specified the encoding used is platform dependent: locale.getpreferredencoding(False) is called to get the current locale encoding. (For reading and writing raw bytes use binary mode and leave encoding unspecified.) The available modes are:

The default mode is 'r' (open for reading text, synonym of 'rt'). For binary read-write access, the mode 'w+b' opens and truncates the file to 0 bytes. 'r+b' opens the file without truncation.

As mentioned in the Overview, Python distinguishes between binary and text I/O. Files opened in binary mode (including 'b' in the mode argument) return contents as bytes objects without any decoding. In text mode (the default, or when 't' is included in the mode argument), the contents of the file are returned as str, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given.

Note

Python doesn’t depend on the underlying operating system’s notion of text files; all the processing is done by Python itself, and is therefore platform-independent.

buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to select line buffering (only usable in text mode), and an integer > 1 to indicate the size in bytes of a fixed-size chunk buffer. When no buffering argument is given, the default buffering policy works as follows:Binary files are buffered in fixed-size chunks; the size of the buffer is chosen using a heuristic trying to determine the underlying device’s “block size” and falling back on io.DEFAULT_BUFFER_SIZE. On many systems, the buffer will typically be 4096 or 8192 bytes long.

“Interactive” text files (files for which isatty() returns True) use line buffering. Other text files use the policy described above for binary files.

encoding is the name of the encoding used to decode or encode the file. This should only be used in text mode. The default encoding is platform dependent (whatever locale.getpreferredencoding() returns), but any text encoding supported by Python can be used. See the codecs module for the list of supported encodings.

errors is an optional string that specifies how encoding and decoding errors are to be handled–this cannot be used in binary mode. A variety of standard error handlers are available (listed under Error Handlers), though any error handling name that has been registered with codecs.register_error() is also valid. The standard names include:'strict' to raise a ValueError exception if there is an encoding error. The default value of None has the same effect.

'ignore' ignores errors. Note that ignoring encoding errors can lead to data loss.

'replace' causes a replacement marker (such as '?') to be inserted where there is malformed data.

'surrogateescape' will represent any incorrect bytes as code points in the Unicode Private Use Area ranging from U+DC80 to U+DCFF. These private code points will then be turned back into the same bytes when the surrogateescape error handler is used when writing data. This is useful for processing files in an unknown encoding.

'xmlcharrefreplace' is only supported when writing to a file. Characters not supported by the encoding are replaced with the appropriate XML character reference nnn;.

'backslashreplace' replaces malformed data by Python’s backslashed escape sequences.

'namereplace' (also only supported when writing) replaces unsupported characters with \N{...} escape sequences.

newline controls how universal newlines mode works (it only applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works as follows:When reading input from the stream, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.

When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.

If closefd is False and a file descriptor rather than a filename was given, the underlying file descriptor will be kept open when the file is closed. If a filename is given closefd must be True (the default) otherwise an error will be raised.

A custom opener can be used by passing a callable as opener. The underlying file descriptor for the file object is then obtained by calling opener with (file, flags). opener must return an open file descriptor (passing os.open as opener results in functionality similar to passing None).

说明:

1. 函数功能打开一个文件,返回一个文件读写对象,然后可以对文件进行相应读写操作。

2. file参数表示的需要打开文件的相对路径(当前工作目录)或者一个绝对路径,当传入路径不存在此文件会报错。或者传入文件的句柄。

>>> a = open('test.txt') # 相对路径

>>> a

>>> a.close()

>>> a = open(r'D:\Python\Python35-32\test.txt') # 绝对路径

>>> a

3. mode参数表示打开文件的模式,常见的打开模式有如下几种,实际调用的时候可以根据情况进行组合。

'r': 以只读模式打开(缺省模式)(必须保证文件存在)

'w':以只写模式打开。若文件存在,则会自动清空文件,然后重新创建;若文件不存在,则新建文件。使用这个模式必须要保证文件所在目录存在,文件可以不存在。该模式下不能使用read*()方法

'a':以追加模式打开。若文件存在,则会追加到文件的末尾;若文件不存在,则新建文件。该模式不能使用read*()方法。

下面四个模式要和上面的模式组合使用

'b':以二进制模式打开

't': 以文本模式打开(缺省模式)

'+':以读写模式打开

'U':以通用换行符模式打开

常见的mode组合

'r'或'rt': 默认模式,文本读模式

'w'或'wt': 以文本写模式打开(打开前文件会被清空)

'rb': 以二进制读模式打开

'ab': 以二进制追加模式打开

'wb': 以二进制写模式打开(打开前文件会被清空)

'r+': 以文本读写模式打开,可以写到文件任何位置;默认写的指针开始指在文件开头, 因此会覆写文件

'w+': 以文本读写模式打开(打开前文件会被清空)。可以使用read*()

'a+': 以文本读写模式打开(写只能写在文件末尾)。可以使用read*()

'rb+': 以二进制读写模式打开

'wb+': 以二进制读写模式打开(打开前文件会被清空)

'ab+': 以二进制读写模式打开

# t为文本读写,b为二进制读写

>>> a = open('test.txt','rt')

>>> a.read()

'some text'

>>> a = open('test.txt','rb')

>>> a.read()

b'some text'

# r为只读,不能写入;w为只写,不能读取

>>> a = open('test.txt','rt')

>>> a.write('more text')

Traceback (most recent call last):

File "", line 1, in

a.write('more text')

io.UnsupportedOperation: write

>>> a = open('test.txt','wt')

>>> a.read()

Traceback (most recent call last):

File "", line 1, in

a.read()

io.UnsupportedOperation: not readable

#其它不一一举例了

4. buffering表示文件在读取操作时使用的缓冲策略。

0: 代表buffer关闭(只适用于二进制模式)

1: 代表line buffer(只适用于文本模式)

>1: 表示初始化的buffer大小

5. encoding参数表示读写文件时所使用的的文件编码格式。

假设现在test.txt文件以utf-8编码存储了一下文本:

>>> a = open('test.txt','rt') # 未正确指定编码,有可能报错

>>> a.read()

Traceback (most recent call last):

File "", line 1, in

a.read()

UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 8: illegal multibyte sequence

>>> a = open('test.txt','rt',encoding = 'utf-8')

>>> a.read()

'我是第1行文本,我将被显示在屏幕\n我是第2行文本,我将被显示在屏幕\n我是第3行文本,我将被显示在屏幕'

>>>

6. errors参数表示读写文件时碰到错误的报错级别。

常见的报错基本有:'strict' 严格级别,字符编码有报错即抛出异常,也是默认的级别,errors参数值传入None按此级别处理.

'ignore' 忽略级别,字符编码有错,忽略掉.

'replace' 替换级别,字符编码有错的,替换成?.

>>> a = open('test.txt','rt',encoding = 'utf-8')

>>> a.read()

'我是第1行文本,我将被显示在屏幕\n我是第2行文本,我将被显示在屏幕\n我是第3行文本,我将被显示在屏幕'

>>> a = open('test.txt','rt')

>>> a.read()

Traceback (most recent call last):

File "", line 1, in

a.read()

UnicodeDecodeError: 'gbk' codec can't decode byte 0xac in position 8: illegal multibyte sequence

>>> a = open('test.txt','rt',errors = 'ignore' )

>>> a.read()

'鎴戞槸绗1琛屾枃鏈锛屾垜灏嗚鏄剧ず鍦ㄥ睆骞\n鎴戞槸绗2琛屾枃鏈锛屾垜灏嗚鏄剧ず鍦ㄥ睆骞\n鎴戞槸绗3琛屾枃鏈锛屾垜灏嗚鏄剧ず鍦ㄥ睆骞'

>>> a = open('test.txt','rt',errors = 'replace' )

>>> a.read()

'鎴戞槸绗�1琛屾枃鏈�锛屾垜灏嗚��鏄剧ず鍦ㄥ睆骞�\n鎴戞槸绗�2琛屾枃鏈�锛屾垜灏嗚��鏄剧ず鍦ㄥ睆骞�\n鎴戞槸绗�3琛屾枃鏈�锛屾垜灏嗚��鏄剧ず鍦ㄥ睆骞�'

7. newline表示用于区分换行符(只对文本模式有效,可以取的值有None,'\n','\r','','\r\n')

>>> a = open('test.txt','rt',encoding = 'utf-8',newline = '\r')

>>> a.readline()

'我是第1行文本,我将被显示在屏幕\r'

>>> a = open('test.txt','rt',encoding = 'utf-8',newline = '\n')

>>> a.readline()

'我是第1行文本,我将被显示在屏幕\r\n'

8. closefd表示传入的file参数类型(缺省为True),传入文件路径时一定为True,传入文件句柄则为False。

>>> a = open('test.txt','rt',encoding = 'utf-8',newline = '\n',closefd = False)

Traceback (most recent call last):

File "", line 1, in

a = open('test.txt','rt',encoding = 'utf-8',newline = '\n',closefd = False)

ValueError: Cannot use closefd=False with file name

>>> a = open('test.txt','rt',encoding = 'utf-8',newline = '\n',closefd = True)

推荐学习视频:轻松学习Python 69个内置函数(持续更新中)​edu.csdn.net

python done函数_【转】Python内置函数(47)——open相关推荐

  1. python常用内置函数乘法_每个 Python 高手都应该知道的内置函数

    每个 Python 高手都应该知道的内置函数 Python 将纳入高中教材,大学 VB 将被 Python 取代, 现在你还对 Python 一无所知吗?去年就被国外一机构预测, 2017 年 pyt ...

  2. Python培训教程:Python有哪些比较重要的内置函数?

    学习Python技术或者参加Python工作的小伙伴们应该都知道,在Python编程语言中会经常出现很多内置函数,很少有人清楚这些函数,但是它的功能是不可小觑的,下面小编就为大家详细介绍一下Pytho ...

  3. python一共有多少个内置函数_Python 35个内置函数,你都ok吗?

    Python一共有60多个内置函数,今天先梳理其中35 个 1 abs() 绝对值或复数的模 In [1]: abs(-6) Out[1]: 6 . 2 all() 接受一个迭代器,如果迭代器的所有元 ...

  4. python学习之最常用的内置函数

    python学习之最常用的内置函数 Python 内置函数总共有70余个(通常把内置类也统称为内置函数),覆盖面广,功能强大.不过,对于初学者在初级阶段,掌握下面几个函数是当务之急. (1) 控制台输 ...

  5. python在匿名函数作和_python内置函数和匿名函数

    楔子 在讲新知识之前,我们先来复习复习函数的基础知识. 问:函数怎么调用? 函数名() 如果你们这么说...那你们就对了!好了记住这个事儿别给忘记了,咱们继续谈下一话题... 来你们在自己的环境里打印 ...

  6. python内置函数入门_Python入门-内置函数一

    什么是内置函数?就是python给你提供的拿来直接用的函数,比如print,input等等,截止到python版本3.6.2 python一共提供了68个内置函数,他们就是python直接提供给我们的 ...

  7. Python中10个常用的内置函数

    大家好,我是小张 在 3.8 版本中,Python 解释器共有近 69 个内置函数可供使用,有了它们能极大地提高编码效率, 数量虽然不少,但在日常搬砖中只用到其中一部分,根据使用频率和用法,列出来几个 ...

  8. python内置函数及方法_python 内置函数 应用及方法

    一.主要内容: 1.内置函数 什么是内置函数? 就是python给你提供的. 拿来直接⽤的函数, 比如print., input等等. 截⽌ 到python版本3.6.2 python⼀共提供了68个 ...

  9. 不是python内置函数的是_Python内置函数

    print(abs(2)) #求出绝对值 print(all(([],1,3))) #传1个可跌倒对象,元素中包含的全部为可迭代对象,返回True 其他返回False #如果可迭代对象是空,就返回Tr ...

  10. mysql数据库内置函数大全_(MariaDB)MySQL内置函数大全

    本文目录: 1. 字符串函数 1.1 字符串连接函数 1.2 lower().upper().left().right() 1.3 填充函数lpad()和rpad() 1.4 trim().ltrim ...

最新文章

  1. AI产品经理入门手册(下)
  2. 在IDEA 中用maven创建web项目
  3. 华农计算机科学宿舍在哪个区,新生指南Ⅵ | 住在华农·看看我们第二个家
  4. 0运维?微信小程序云开发增删查改【05】
  5. python硬件_「大神器!」硬件的AI性能测试Python库发布
  6. 【hdu2825】ac自动机 + 状压dp
  7. Web前端笔记-圆环随时间逐渐缩小(使用two.js)
  8. 面试题——栈的压入、弹出顺序
  9. 各国选手Techfest备战FMB世界杯,极战远征军斩获佳绩为国争光
  10. POJ 1195 Mobile phones (二维树状数组)
  11. python学习笔记9:面向对象编程,类
  12. 古风宣纸背景教学课件讲座PPT模板
  13. 魔兽世界插件编写-第一个插件-空插件 EmptyAddOns
  14. IEEE Transactions on Systems, Man, and Cybernetics: Systems(TSMC)投稿须知
  15. 微信群发可以分组吗?群组标签分组设置
  16. java包是什么意思_java中的“包”到底是什么意思?
  17. 程序员想拿高薪,你还缺了这些,在寒冬下如何选择?创业or进阶
  18. ping请求超时问题研究
  19. 【pg Postgres】 Postgres解决Permission denied for relation
  20. 【网络游戏§绿色DOTA2魔笛V1.001 官方最新版§DOTA游戏辅助工具§】

热门文章

  1. Java 原子操作的实现原理
  2. java txt文件转pdf_java txt转化pdf格式出错
  3. pks300服务器自动同步,霍尼PKS组态培训资料-EPKS300.pdf
  4. Labview写excel的实现
  5. 百度竞价排名的基本知识
  6. 致敬三八女神节,致敬IT女生
  7. 这是一篇关于HaaS 506的小Tips
  8. 电脑远程管理手机文件夹
  9. MongoDB特定类型的查询
  10. VOC数据增强与调整大小