1. 程式人生 > >python 文件操作--內置對象open

python 文件操作--內置對象open

trac tool .text imp 缺省 support span seq com

說明:

  1. 函數功能打開一個文件,返回一個文件讀寫對象,然後可以對文件進行相應讀寫操作。

  2. file參數表示的需要打開文件的相對路徑(當前工作目錄)或者一個絕對路徑,當傳入路徑不存在此文件會報錯。或者傳入文件的句柄。

技術分享
>>> a = open(‘test.txt‘) # 相對路徑
>>> a
<_io.TextIOWrapper name=‘test.txt‘ mode=‘r‘ encoding=‘cp936‘>
>>> a.close()

>>> a = open(r‘D:\Python\Python35-32\test.txt‘) # 絕對路徑
>>> a
<_io.TextIOWrapper name=‘D:\\Python\\Python35-32\\test.txt‘ mode=‘r‘ encoding=‘cp936‘>
技術分享

  3. mode參數表示打開文件的模式,常見的打開模式有如下幾種,實際調用的時候可以根據情況進行組合。

    

打開文件時,需要指定文件路徑和以何等方式打開文件,打開後,即可獲取該文件句柄,日後通過此文件句柄對該文件操作。

打開文件的模式有:

  • r ,只讀模式【默認】
  • w,只寫模式【不可讀;不存在則創建;存在則清空內容;】
  • x, 只寫模式【不可讀;不存在則創建,存在則報錯】
  • a, 追加模式【可讀; 不存在則創建;存在則只追加內容;】

"+" 表示可以同時讀寫某個文件

  • r+, 讀寫【可讀,可寫】
  • w+,寫讀【可讀,可寫】
  • x+ ,寫讀【可讀,可寫】
  • a+, 寫讀【可讀,可寫】

"b"表示以字節的方式操作

  • rb 或 r+b
  • wb 或 w+b
  • xb 或 w+b
  • ab 或 a+b

註:以b方式打開時,讀取到的內容是字節類型,寫入時也需要提供字節類型

# t為文本讀寫,b為二進制讀寫
>>> a = open(‘test.txt‘,‘rt‘)
>>> a.read()
‘some text‘
>>> a = open(‘test.txt‘,‘rb‘)
>>> a.read()
b‘some text‘

# r為只讀,不能寫入;w為只寫,不能讀取
>>> a = open(‘test.txt‘,‘rt‘)
>>> a.write(‘more text‘)
Traceback (most recent call last):
  File "<pyshell#67>", line 1, in <module>
    a.write(‘more text‘)
io.UnsupportedOperation: write
>>> a = open(‘test.txt‘,‘wt‘)
>>> a.read()
Traceback (most recent call last):
  File "<pyshell#69>", line 1, in <module>
    a.read()
io.UnsupportedOperation: not readable

#其它不一一舉例了

  4. buffering表示文件在讀取操作時使用的緩沖策略。

      0: 代表buffer關閉(只適用於二進制模式)
      1: 代表line buffer(只適用於文本模式)
      >1: 表示初始化的buffer大小

  5. encoding參數表示讀寫文件時所使用的的文件編碼格式。

  假設現在test.txt文件以utf-8編碼存儲了一下文本:

技術分享

技術分享
>>> a = open(‘test.txt‘,‘rt‘) # 未正確指定編碼,有可能報錯
>>> a.read()
Traceback (most recent call last):
  File "<pyshell#87>", line 1, in <module>
    a.read()
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xac in position 8: illegal multibyte sequence

>>> a = open(‘test.txt‘,‘rt‘,encoding = ‘utf-8‘)
>>> a.read()
‘我是第1行文本,我將被顯示在屏幕\n我是第2行文本,我將被顯示在屏幕\n我是第3行文本,我將被顯示在屏幕‘
>>> 
技術分享

  6. errors參數表示讀寫文件時碰到錯誤的報錯級別。

  常見的報錯基本有:

  • ‘strict‘ 嚴格級別,字符編碼有報錯即拋出異常,也是默認的級別,errors參數值傳入None按此級別處理.
  • ‘ignore‘ 忽略級別,字符編碼有錯,忽略掉.
  • ‘replace‘ 替換級別,字符編碼有錯的,替換成?.
技術分享
>>> a = open(‘test.txt‘,‘rt‘,encoding = ‘utf-8‘)
>>> a.read()
‘我是第1行文本,我將被顯示在屏幕\n我是第2行文本,我將被顯示在屏幕\n我是第3行文本,我將被顯示在屏幕‘
>>> a = open(‘test.txt‘,‘rt‘)
>>> a.read()
Traceback (most recent call last):
  File "<pyshell#91>", line 1, in <module>
    a.read()
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xac in position 8: illegal multibyte sequence
>>> a = open(‘test.txt‘,‘rt‘,errors = ‘ignore‘ )
>>> a.read()
>>> a = open(‘test.txt‘,‘rt‘,errors = ‘replace‘ )
>>> a.read()
技術分享

  7. newline表示用於區分換行符(只對文本模式有效,可以取的值有None,‘\n‘,‘\r‘,‘‘,‘\r\n‘)

>>> a = open(‘test.txt‘,‘rt‘,encoding = ‘utf-8‘,newline = ‘\r‘)
>>> a.readline()
‘我是第1行文本,我將被顯示在屏幕\r‘
>>> a = open(‘test.txt‘,‘rt‘,encoding = ‘utf-8‘,newline = ‘\n‘)
>>> a.readline()
‘我是第1行文本,我將被顯示在屏幕\r\n‘

  8. closefd表示傳入的file參數類型(缺省為True),傳入文件路徑時一定為True,傳入文件句柄則為False。

>>> a = open(‘test.txt‘,‘rt‘,encoding = ‘utf-8‘,newline = ‘\n‘,closefd = False)
Traceback (most recent call last):
  File "<pyshell#115>", line 1, in <module>
    a = open(‘test.txt‘,‘rt‘,encoding = ‘utf-8‘,newline = ‘\n‘,closefd = False)
ValueError: Cannot use closefd=False with file name
>>> a = open(‘test.txt‘,‘rt‘,encoding = ‘utf-8‘,newline = ‘\n‘,closefd = True)

python 文件操作--內置對象open