標籤:寫檔案 檔案讀寫 write 檔案 close fse date start 表示
## 開啟一個檔案
- fileobj = open(filename, mode)
其中:
fileobj是open()返回的檔案對象
filename是該檔案的字串名
mode是指明檔案類型和操作的子字串
- mode的第一個字母表示對其的操作
- r表示讀取
- w表示寫入,如果檔案不存在就建立,如果存在則重新寫入新內容
- x表示在檔案不存在的情況下新建立並寫檔案
- a表示如果檔案存在,在檔案末尾追加內容
- mode的第二個字母是檔案類型
- t(或者省略)代表文本類型
- b代表二進位檔案
## 使用write()寫入檔案,使用close()關閉檔案
1 poem = """2 離離原上草,一歲一枯榮。3 野火燒不盡,春風吹又生。4 遠芳侵古道,晴翠接荒城。5 又送王孫去,萋萋滿別情。6 """7 file_obj = open("詩", ‘wt‘) # 開啟名為‘詩’的文字檔,若‘詩’不存在則建立8 file_obj.write(poem) # 寫入檔案9 file_obj.close() # 關閉檔案
- 也可以使用print()寫入檔案
1 poem = """2 離離原上草,一歲一枯榮。3 野火燒不盡,春風吹又生。4 遠芳侵古道,晴翠接荒城。5 又送王孫去,萋萋滿別情。6 """7 file_obj = open("詩", ‘wt‘) # 開啟名為‘詩’的文字檔,若‘詩’不存在則建立8 print(poem, file=file_obj) # 寫入檔案9 file_obj.close() # 關閉檔案
- 如果需要寫入的內容非常多,可以將資料分塊寫入
1 poem = """ 2 離離原上草,一歲一枯榮。 3 野火燒不盡,春風吹又生。 4 遠芳侵古道,晴翠接荒城。 5 又送王孫去,萋萋滿別情。 6 """ 7 file_obj = open("詩", ‘wt‘) # 開啟名為‘詩’的文字檔,若‘詩’不存在則建立 8 9 start = 010 chunk = 20 # 每次寫入20個字元11 while True:12 if start > len(poem): 13 break14 file_obj.write(poem[start:start+chunk])15 start += chunk16 17 file_obj.close() # 關閉檔案
## 檔案讀取
- read():不帶參數的read()函數一次讀取檔案的所有內容
1 file_obj = open("詩", ‘rt‘) # 使用讀模數式開啟文字檔 2 poem = file_obj.read() # 讀取檔案中所有內容 3 file_obj.close() # 關閉檔案 4 print(poem) 5 """ 6 輸出: 7 離離原上草,一歲一枯榮。 8 野火燒不盡,春風吹又生。 9 遠芳侵古道,晴翠接荒城。10 又送王孫去,萋萋滿別情。11 """
- 同樣可以分塊讀取
1 poem = ‘‘ 2 file_onj = open("詩", ‘rt‘) 3 chunk = 20 # 每次讀取20個字元 4 while True: 5 frag = file_onj.read(chunk) 6 if not frag: # 讀到檔案末尾時再次調用read()函數會返回Null 字元串,not frag為真,執行break 7 break 8 poem += frag 9 10 print(poem)11 """12 輸出:13 離離原上草,一歲一枯榮。14 野火燒不盡,春風吹又生。15 遠芳侵古道,晴翠接荒城。16 又送王孫去,萋萋滿別情。17 """
- 一行一行的讀取
1 poem = ‘‘ 2 file_obj = open(‘詩‘, ‘rt‘) 3 while True: 4 line = file_obj.readline() 5 if not line: 6 break 7 poem += line 8 9 print(poem)10 """11 輸出:12 離離原上草,一歲一枯榮。13 野火燒不盡,春風吹又生。14 遠芳侵古道,晴翠接荒城。15 又送王孫去,萋萋滿別情。16 """
- 使用迭代器讀取檔案
1 poem = ‘‘ 2 file_obj = open("詩", ‘rt‘) 3 for line in file_obj: # 效果相同,但代碼更短 4 poem += line 5 6 print(poem) 7 """ 8 輸出: 9 離離原上草,一歲一枯榮。10 野火燒不盡,春風吹又生。11 遠芳侵古道,晴翠接荒城。12 又送王孫去,萋萋滿別情。13 """
- readlines():每次讀取一行,並返回單行字串的列表
1 file_obj = open("詩", ‘rt‘) 2 lines = file_obj.readlines() 3 print(lines) 4 for line in lines: 5 print(line, end=‘‘) 6 7 """ 8 [‘\n‘, ‘ 離離原上草,一歲一枯榮。\n‘, ‘ 野火燒不盡,春風吹又生。\n‘, ‘ 遠芳侵古道,晴翠接荒城。\n‘, ‘ 又送王孫去,萋萋滿別情。\n‘] 9 10 離離原上草,一歲一枯榮。11 野火燒不盡,春風吹又生。12 遠芳侵古道,晴翠接荒城。13 又送王孫去,萋萋滿別情。14 """
## write()寫入二進位檔案
1 bin_data = bytes(range(0, 255)) # 將序列轉換為位元據2 f = open(‘bin_file‘, ‘wb‘) # 以二進位檔案的格式開啟檔案3 f.write(bin_data) # 寫入4 f.close() # 關閉
- 分段寫入
1 bin_data = bytes(range(0, 255)) 2 f = open(‘bin_file‘, ‘wb‘) 3 chunk = 100 # 每次寫入100個位元組 4 offset = 0 5 while True: 6 if offset > len(bin_date): 7 break 8 f.write(bin_data[offset:chunk + offset]) 9 offset += chunk10 11 f.close()
- 讀取二進位檔案
1 f = open(‘bin_file‘, ‘rb‘)2 bin_data = f.read()3 f.close()
## 使用with自動關閉檔案,with代碼塊執行完畢後自動關閉檔案
1 with open("詩", ‘rt‘) as f: 2 poem = f.read() 3 4 print(poem) 5 """ 6 輸出: 7 離離原上草,一歲一枯榮。 8 野火燒不盡,春風吹又生。 9 遠芳侵古道,晴翠接荒城。10 又送王孫去,萋萋滿別情。11 """
本文參考:
[美]Bill Lubanovic 《Python語言及其應用》
Python檔案IO(普通檔案讀寫)