Python3中的open函数

简介:

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

    Open file and return a stream.  Raise IOError upon failure.

#打开文件并返回一个流?失败则抛出IOError异常

mode:

    ========= ===============================================================

    Character Meaning

    --------- ---------------------------------------------------------------

    'r'       open for reading (default)

    'w'       open for writing, truncating the file first

    'x'       create a new file and open it for writing

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

    'U'       universal newline mode (deprecated)

    ========= ===============================================================

mode不使用参数默认是'rt',‘w’写模式,会覆盖原来全部的内容(会创建文件),‘x’创建一个新的文件,并写入内容如果文件存在会‘FileExistsError’,‘a’在文件末尾追加内容,‘b’二进制模式,‘+’更新磁盘文件(读写),‘U’弃用

参数有a和w会创建不存在的文件


buffering:

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

0 只能用在二进制模式

1 行缓冲

>1 则使用给定的值做缓冲大小

*在没有给出参数的情况下,二进制文件的大小有底层设备“block size”决定,可以通过‘io.DEFAULT_BUFFER_SIZE’获取,在很多系统中这个值的大小为4096或者8192字节

*文本文件则采用行缓冲

encoding:

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, but any encoding supported by Python can be

    passed.  See the codecs module for the list of supported encodings.

encoding是文件的解码或者编码方式,只能用于文本模式,默认的编码方式依赖于平台,任何python能够支持编码都可以在python中使用,可以查看编码模块


errors:

errors is an optional string that specifies how encoding errors are to

    be handled---this argument should not be used in binary mode. Pass

    'strict' to raise a ValueError exception if there is an encoding error

    (the default of None has the same effect), or pass 'ignore' to ignore

    errors. (Note that ignoring encoding errors can lead to data loss.)

    See the documentation for codecs.register or run 'help(codecs.Codec)'

    for a list of the permitted encoding error strings.

errors是一个可选的参数,并且不能用于二进制模式,如果出现编码错误会排出ValueError错误,或者使用‘ignoe’忽略,可通过查看codecs.codec获取错误编码字符串


newline:

newline controls how universal newlines works (it only applies to text

    mode). It can be None, '', '\n', '\r', and '\r\n'.  It works as

    follows:

    

    * On input, 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 newline 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.

    

    * On output, 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.

换行控制,参数可以用None, '', '\n', '\r', and '\r\n'(只能用于文本模式)

*输入时,

如果参数为None,那么换行符启用,结尾可以是'\n', '\r', or '\r\n',并且这些控制符都会编码为'\n'。

如果是''换行符模式启用,但是行位的换行符在返回调用时将不会被编码。

如果给出其他有效参数,返回调用时将会使用指定的参数

*输出时,

如果参数为None,任何‘\n’将会编码成系统默认的分隔符

如果参数为‘’或者'\n',将不会编码

如果参数为其他有效值,'\n'将会编码成给定的值


closefd:

If closefd is False, the underlying file descriptor will be kept open

    when the file is closed. This does not work when a file name is given

    and must be True in that case.

当文件关闭时,如果closefd为False,底层文件描述仍然是打开,设置为True底层文件描述同时也会关闭。


opener:

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

可以通过调用*opener*来自定义opener,底层文件是通过调用*opener*, *file*, *flags*来获取描述。*opener*必须返回一个打开的文件描述。os.open作为*opener*的返回结果类似于通过None。


 open() returns a file object whose type depends on the mode, and

    through which the standard file operations such as reading and writing

    are performed. When open() is used to open a file in a text mode ('w',

    'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open

    a file in a binary mode, the returned class varies: in read binary

    mode, it returns a BufferedReader; in write binary and append binary

    modes, it returns a BufferedWriter, and in read/write mode, it returns

    a BufferedRandom.

    

    It is also possible to use a string or bytearray as a file for both

    reading and writing. For strings StringIO can be used like a file

    opened in a text mode, and for bytes a BytesIO can be used like a file

    opened in a binary mode.


1
2
3
4
5
6
7
8
9
10
11
12
13
:~ /Code cat  opentest
pythonis a  open  testthis is ab
abc
edf
dfc
dag
dagk
asgg
asdgag
aggfdn
sdnhsdfo
sdfigsodfnh
****

使用r+的结果

eg.

1
2
3
4
5
6
7
8
>>> f  =  open ( 'opentest' 'r+' )
>>> f.write( '1111' )
4
>>> f.write( '2222' )
4
>>> f.write( '3333' )
4
>>> f.close()


再次查看opentest内容

1
2
3
4
5
6
7
8
9
10
11
12
13
:~ /Code cat  opentest
111122223333pen testthis is ab
abc
edf
dfc
dag
dagk
asgg
asdgag
aggfdn
sdnhsdfo
sdfigsodfnh
****

使用r+,指针在开头,会覆盖掉原位置原有的内容




     本文转自forsk 51CTO博客,原文链接:http://blog.51cto.com/forsk/2059134,如需转载请自行联系原作者



相关文章
|
2月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
227 1
|
2月前
|
算法 Java Docker
(Python基础)新时代语言!一起学习Python吧!(三):IF条件判断和match匹配;Python中的循环:for...in、while循环;循环操作关键字;Python函数使用方法
IF 条件判断 使用if语句,对条件进行判断 true则执行代码块缩进语句 false则不执行代码块缩进语句,如果有else 或 elif 则进入相应的规则中执行
337 1
|
2月前
|
Java 数据处理 索引
(numpy)Python做数据处理必备框架!(二):ndarray切片的使用与运算;常见的ndarray函数:平方根、正余弦、自然对数、指数、幂等运算;统计函数:方差、均值、极差;比较函数...
ndarray切片 索引从0开始 索引/切片类型 描述/用法 基本索引 通过整数索引直接访问元素。 行/列切片 使用冒号:切片语法选择行或列的子集 连续切片 从起始索引到结束索引按步长切片 使用slice函数 通过slice(start,stop,strp)定义切片规则 布尔索引 通过布尔条件筛选满足条件的元素。支持逻辑运算符 &、|。
195 0
|
3月前
|
设计模式 缓存 监控
Python装饰器:优雅增强函数功能
Python装饰器:优雅增强函数功能
286 101
|
3月前
|
数据库连接 Python
超越`open()`:深入理解Python的`with`语句
超越`open()`:深入理解Python的`with`语句
411 99
|
3月前
|
缓存 测试技术 Python
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
236 99
|
3月前
|
存储 缓存 测试技术
Python装饰器:优雅地增强函数功能
Python装饰器:优雅地增强函数功能
206 98
|
3月前
|
缓存 Python
Python中的装饰器:优雅地增强函数功能
Python中的装饰器:优雅地增强函数功能
|
4月前
|
Python
Python 函数定义
Python 函数定义
590 155
|
5月前
|
PHP Python
Python format()函数高级字符串格式化详解
在 Python 中,字符串格式化是一个重要的主题,format() 函数作为一种灵活且强大的字符串格式化方法,被广泛应用。format() 函数不仅能实现基本的插入变量,还支持更多高级的格式化功能,包括数字格式、对齐、填充、日期时间格式、嵌套字段等。 今天我们将深入解析 format() 函数的高级用法,帮助你在实际编程中更高效地处理字符串格式化。
597 0

推荐镜像

更多