Python 中 open() 的 mode 参数总结

参考 : ide

http://www.newsmth.net/nForum/#!article/Python/54311函数

pydoc 中的 file 一节.net


Python 中打开一个文件的函数 open() 的第二个参数指明了打开的模式. 能够赋予该参数的实参 *枚举* 以下 : 指针

'r', 'r+', 'w', 'w+', 'a', 'a+', 'rU'对象


其中 : it

'r' : 以只读模式打开class

'r+' : 以读写模式打开, 写的指针刚开始指在文件开头, 所以会覆写文件file

'w' : 清空文件, 不能使用 <file>.read*() 方法.  (read(), readline(), readlines())方法

'w+' : 清空文件, 可使用 <file>.read*() 方法. di

'a' : 向一个文件追加内容,  不能使用 .read*()

'a+' : 向一个文件追加内容, 可使用 .read*()

'rU' : 自动探测行尾符, 若是以这种模式打开一个文件, 该文件对象将得到一个 newlines 属性, 当还未读到任何行尾时, <file>.newlines 属性等于 None, 读到了则多是'\n', '\r\n', '\r' 中的一种. (什么系统的换行符是 \r, 求高手指教!!!)


注 : 1. 读的指针老是指在文件开头

     2. <file>.seek() 改变的老是读的指针

相关文章
相关标签/搜索