码迷,mamicode.com
首页 > 其他好文 > 详细

操作文件

时间:2017-08-13 20:52:13      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:http   erro   com   bytes   rect   模式   obj   htm   cal   

open(filemode=‘r‘buffering=-1encoding=Noneerrors=Nonenewline=Noneclosefd=Trueopener=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:

CharacterMeaning
‘r‘ open for reading (default)。读操作
‘w‘ open for writing, truncating the file first  打开文件进行写操作,如果文件已经存在,会清空其内容  
‘x‘

open for exclusive creation, failing if the file already exists  python3.5 新增加的模式,独占创建文件。如果文件已经存在,则创建失败。

‘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)  打开文件进行更新操作,也就是支持对文件进行读写操作。注意文件操作指针的位置。

read 和write操作都会移动当前文件的操作指针。例如write(‘haha‘),会覆盖当前指针所在位置后面的4个位置,这时指针也移动了4个位置。

read操作同样也会移动指针位置。

w+:

该模式依然是会在打开文件的同时清空文件的内容。请注意。

 

 

‘U‘ universal newlines mode (deprecated)

操作文件

标签:http   erro   com   bytes   rect   模式   obj   htm   cal   

原文地址:http://www.cnblogs.com/pengxuann/p/7339434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!