1. 程式人生 > >day 8 - 1 檔案操作

day 8 - 1 檔案操作

檔案操作

注意:

1. r+ 最為常用

2.encoding 的編碼格式一定要與檔案編碼格式一致

讀取 r  rb

#在本地建立 txt 格式的檔案預設使用 gbk 格式
f = open('e:/py/file.txt',mode='r',encoding='gbk')
content = f.read()
print(content,type(content))
f.close()

# b 二進位制模式 bytes
f= open('e:/py/file.txt',mode='rb')
content = f.read()
print(content,type(content))
f.close()

只寫  w  wb

#只寫  w
#我們寫入的格式為 utf-8 那麼當我們檢視時也要使用 utf-8 格式
#沒有該檔案 w 會建立檔案
f = open('e:/py/test.txt',mode='w',encoding='utf-8')
f.write('正在寫入...')
f.close()


#存在該檔案 會覆蓋寫入
#即只寫的邏輯為 先清空檔案在寫入
f = open('e:/py/test.txt',mode='w',encoding='utf-8')
f.write('已寫入.')
f.close()


#wb 寫入
#預設寫入 bytes 型別 需要使用 encode 轉換為 str 型別
#寫入與檔案預設格式不同的型別 如:utf-8 會自動轉換 f = open('e:/py/test.txt',mode='wb') f.write('sfssg個人是的.'.encode('gbk')) f.close()

追加  a  ab

#追加  a
#追加與檔案預設格式不同的型別 如:utf-8 不會自動轉換
#預設會自動追加在檔案游標的位置(即有文字的最後一位上)
f = open('e:/py/test.txt',mode='a',encoding='gbk')
f.write('追加...')
f.close()


#ab 追加
#追加與檔案預設格式不同的型別 如:utf-8 不會自動轉換
f = open('e:/py/test.txt',mode='ab') f.write('方式...'.encode('utf-8')) f.close()

讀寫   r+  最常用

#讀寫時的檔案必須和原始檔編碼一致
#讀寫的位置是在讀完後的最有一個字元後面
#試想在 r+ 模式下 改變讀寫順序
#會在檔案開頭寫入 沒寫入一個字元變會替換一個字元
f = open('e:/py/test.txt',mode='r+',encoding='utf-8')
print(f.read())
f.write("...")
f.close()


#r+ 也存在 bytes 型別 r+b
#要與原始檔編碼一致
f = open('e:/py/test.txt',mode='r+b')
print(f.read())
f.write("你就能看".encode('gbk'))
f.close()

寫讀  w+ 不常用

#還是先清除 在新增  所以沒什麼用
#w+ 也存在 bytes 型別 w+b
f = open('e:/py/test.txt',mode='w+',encoding='utf-8')
f.write("...")
print(f.read())
f.close()

#w+ 與 seek 調節游標
#沒什麼用
f = open('e:/py/test.txt',mode='w+',encoding='utf-8')
f.write("...")
f.seek(0) #游標在 0 的位置
print(f.read()) #讀出結果
f.close()

追加 a+

#與 a 的區別在於 a 只可以執行一個動作
#a+ 可以執行多步
#a+ 也存在 bytes 型別 a+b
f = open('e:/py/test.txt',mode='a+',encoding='gbk')
f.write('追加...')
print(f.read())  #這樣仍然讀不到內容 因為游標在最後
f.close()


# a+ 與 seek
f = open('e:/py/test.txt',mode='a+',encoding='gbk')
f.write('追加...')
f.seek(0)  #游標置於 0 的位置
print(f.read())
f.close()

功能詳解

read 與 seek 

#按照字元讀出  read 讀出來的都是字元
#字元是能看到的最小單位
#要與原始檔編碼一致
f = open('e:/py/test.txt',mode='r+',encoding='gbk')
print(f.read(3))
f.close()


#read 結合 seek
#seek 是按照位元組來讀取的
f = open('e:/py/test.txt',mode='r+',encoding='gbk')
f.seek(4)  #gbk 每個漢字 2 個位元組  utf-8 每個漢字 3 個位元組
print(f.read())
f.close()

tell 獲取游標位置

f = open('e:/py/test.txt',mode='r+',encoding='gbk')
f.seek(4)  #按照位元組定義游標位置
print(f.tell())
f.close()

移動游標讀取後三個字

f = open('d:/py/file.txt',mode='r+',encoding='utf-8')
f.readline()  #讀取一行使游標移動到末尾
f.seek((f.tell())-9)  #先鎖定游標位置 在進行移動
print(f.read())
f.close()

readable、readline、readlines 與 truncate

f = open('e:/py/test.txt',mode='r+',encoding='gbk')
#print(f.readable())      #是否可讀  返回值:true false
#print(f.readline())      #每次讀一行,即一行一行的讀
#print(f.readlines())     #把每一行當做一個元素,新增到 list 中
f.truncate(2)             #對原始檔擷取一段資料用來更改,並覆蓋原始檔
f.close() 

 讀取第二行內容

f = open('e:/py/test.txt',mode='r+',encoding='gbk')
f.readline()     
f.seek(f.tell())
print(f.readline())
f.close()

讀整個檔案

#for 迴圈寫法
#迴圈打印出每一行(包括換行符)
#讀檔案時 要分段讀 因為你不知道檔案有多大
f = open('e:/py/test.txt',mode='r+',encoding='gbk')
for i in f:
    print(i)
f.close()


#with 寫法(自動關閉——close)
#可以同時開啟多個檔案
with open('e:/py/test.txt',mode='r+',encoding='gbk') as file:
    print(file.read())

with open('e:/py/test.txt',mode='r+',encoding='gbk') as file,\
open('e:/py/file.txt',mode='r+',encoding='gbk') as file2:
    print(file.read())
    print(file2.read())