python open的用法_python 中open()的用法?

本文详细介绍了文件操作的各种模式,包括只读(r)、可写(w)、追加(a)等基本模式,以及如何通过添加二进制(b)、文本(t)、同时读写(+)等标识来扩展这些基本模式的功能。此外,还介绍了缓冲设置及其作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2017-05-19 回答

r表示只读,b表示二进制

与此对应的是w表示可写,t表示文本方式打开。

再增加一些官方的解释:

>>> print file.__doc__

file(name[, mode[, buffering]]) -> file object

open a file. the mode can be 'r', 'w' or 'a' for reading (default),

writing or appending. the file will be created if it doesn't exist

when opened for writing or appending; it will be truncated when

opened for writing. add a 'b' to the mode for binary files.

add a '+' to the mode to allow simultaneous reading and writing.

if the buffering argument is given, 0 means unbuffered, 1 means line

buffered, and larger numbers specify the buffer size. the preferred way

to open a file is with the builtin open() function.

add a 'u' to mode to open the file for input with universal newline

support. any line ending in the input file will be seen as a '\n'

in python. also, a file so opened gains the attribute 'newlines';

the value for this attribute is one of none (no newline read yet),

'\r', '\n', '\r\n' or a tuple containing all the newline types seen.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值