Python3.6内置函数(47)——open

英文文档 open(file, mode='r', buffering=-1, encoding=N

英文文档

  • 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, anOSErroris raised.

    open()

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

    使用指定的模式和编码打开文件,返回文件读写对象。

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

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

    """

    Python IDLE或shell中切换路径

    在Python自带的编辑器IDLE中或者Python shell中不能使用cd命令,那么跳到目标路径呢。

    方法是使用os包下的相关函数实现路径切换功能。

    import os

    os.getcwd() #获取当前路径 #'D:Python36'

    os.chdir("D:est") #跳到目标路径下

    os.chdir('D:est') #单引号、双引号都可以

    """

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

    >>> a

    <_io.TextIOWrapper name='test.txt' mode='r' encoding='cp936'>

    >>> a.close()

    >>> a = open(r'D:Python36est.txt')# 绝对路径

    >>> a

    <_io.TextIOWrapper name='D:Python36est.txt' mode='r' encoding='cp936'>

    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()

    'test text'

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

    >>> a.read()

    b'test text'

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

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

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

    Traceback (most recent call last):

    File " " , line 1, in <module>

    a.write('more text')

    io.UnsupportedOperation: not writable

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

    >>> a.read()

    Traceback (most recent call last):

    File " " , line 1, in <module>

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

    a.read()

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

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

    >>> a.read()

    'open第一行数据open第二行数据open第三行数据'

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

    常见的报错基本有:

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

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

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

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

    >>> a.read()

    'open第一行数据open第二行数据open第三行数据'

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

    >>> a.read()

    Traceback (most recent call last):

    File " " , line 1, in <module>

    a.read()

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

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

    >>> a.read()

    'open绗涓琛屾暟鎹open绗浜岃屾暟鎹open绗涓夎屾暟鎹'

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

    >>> a.read()

    'open绗�涓�琛屾暟鎹�open绗�浜岃�屾暟鎹�open绗�涓夎�屾暟鎹�'

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

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

    >>> a.readline()

    'open第一行数据'

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

    >>> a.readline()

    'open第一行数据'

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

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

    Traceback (most recent call last):

    File " " , line 1, in

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

    ValueError: Cannot use closefd=False with file name

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

打开APP阅读更多精彩内容