Python文件操作

open(...)
  open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) -> file object
  Open file and return a stream. Raise IOError upon failure.

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

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

The default mode is 'rt' (open for reading text). For binary random access, the mode 'w+b' opens and truncates the file to 0 bytes, while 'r+b' opens the file without truncation.

The 'x' mode implies 'w' and raises an `FileExistsError` if the file already exists.

文件的打开模式:

 

文件的对象方法:

posted @ 2016-08-25 23:18  shiddong  阅读(233)  评论(0编辑  收藏  举报