python:從入門到放棄 06 檔案處理
阿新 • • 發佈:2022-03-14
目錄
什麼是檔案
在進行檔案處理之前,我們需要知道是什麼是檔案,檔案即作業系統提供給應用程式來操作硬碟的虛擬概念,使用者或應用程式對檔案的操作,就是向作業系統發起呼叫,然後由作業系統完成對硬碟的具體操作。
檔案處理流程
1.開啟檔案、建立檔案
2.編輯檔案內容
3.儲存檔案內容
4.關閉檔案
檔案的開啟與關閉
f = open() # 開啟 f = close() # 關閉 1.使用關鍵字開啟檔案 open(r'a.txt') # 相對路徑 open(r'D:\py1\day09\a.txt') # 絕對路徑 res = open(r'a.txt', 'r', encoding='utf8') ''' open(檔案的路徑,檔案的操作模式,檔案的編碼) 1.檔案的路徑是必須要寫的 2.檔案的操作模式、檔案的編碼有時候不用寫 ''' 在處理完檔案後,我們需要使用close()關閉檔案,然而這一個步驟非常的容易遺忘掉。 考慮到這一點,python提供了with關鍵字來幫我們管理上下文 # 1、在執行完子程式碼塊後,with 會自動執行f.close() with open('a.txt','w') as f: pass # 2、可用用with同時開啟多個檔案,用逗號分隔開即可 with open('a.txt','r') as read_f,open('b.txt','w') as write_f: data = read_f.read() write_f.write(data)
檔案的讀寫模式
r read 只讀模式:只能讀不能寫
w write 只寫模式:只能寫不能讀
a append 只追加模式:在檔案末尾新增內容
r模式
# r只讀模式: 在檔案不存在時則報錯,檔案存在檔案內指標直接跳到檔案開頭
with open('a.txt',mode='r',encoding='utf-8') as f:
res=f.read() # 會將檔案的內容由硬碟全部讀入記憶體,賦值給res
w模式
# w只寫模式: 在檔案不存在時會建立空文件,檔案存在會清空檔案,檔案指標跑到檔案開頭 with open('b.txt',mode='w',encoding='utf-8') as f: f.write('你好\n') f.write('我好\n') f.write('大家好\n') f.write('111\n222\n333\n') #強調: # 1 在檔案不關閉的情況下,連續的寫入,後寫的內容一定跟在前寫內容的後面 # 2 如果重新以w模式開啟檔案,則會清空檔案內容
a模式
# a只追加寫模式: 在檔案不存在時會建立空文件,檔案存在會將檔案指標直接移動到檔案末尾
with open('c.txt',mode='a',encoding='utf-8') as f:
f.write('44444\n')
f.write('55555\n')
#強調 w 模式與 a 模式的異同:
# 1 相同點:在開啟的檔案不關閉的情況下,連續的寫入,新寫的內容總會跟在前寫的內容之後
# 2 不同點:以 a 模式重新開啟檔案,不會清空原檔案內容,會將檔案指標直接移動到檔案末尾,新寫的內容永遠寫在最後
+模式
r+ w+ a+ :可讀可寫
在平時工作中,我們只單純使用r/w/a,要麼只讀,要麼只寫,一般不用可讀可寫的模式*
檔案的操作模式
t模式
文字模式 是預設的模式
r rt
w wt
a at
1.該模式只能操作文字檔案
2.該模式必須要指定encoding引數
3.該模式讀寫都是以字串為最小單位
b模式
二進位制模式 可以操作任意型別的檔案
rb 不能省略b
wb 不能省略b
ab 不能省略b
1.該模式可以操作任意型別的檔案
2.該模式不需要指定encoding引數
3.該模式讀寫都是以bytes型別為最小單位
檔案的內建方法
# 讀操作
.read() # 讀取所有內容,執行完該操作後,檔案指標會移動到檔案末尾
.readline() # 讀取一行內容,游標移動到第二行首部
.readlines() # 讀取每一行內容,存放於列表中
.readable() # 判斷當前檔案是否可讀
# 強調:
# f.read()與f.readlines()都是將內容一次性讀入內容,如果內容過大會導致記憶體溢位,若還想將內容全讀入記憶體,則必須分多次讀入,有兩種實現方式:
# 方式一
with open('a.txt',mode='rt',encoding='utf-8') as f:
for line in f:
print(line) # 同一時刻只讀入一行內容到記憶體中
# 方式二
with open('1.mp4',mode='rb') as f:
while True:
data=f.read(1024) # 同一時刻只讀入1024個Bytes到記憶體中
if len(data) == 0:
break
print(data)
# 寫操作
.write('1111\n222\n') # 針對文字模式的寫,需要自己寫換行符
.writelines(['333\n','444\n']) # 檔案模式
.writable() # 判斷檔案是否可寫
.flush() # 相當於主動按了ctrl+s(儲存)
小作業
1.編寫一個簡易版本的拷貝程式
路徑全部自定義
file_path = input(r'輸入檔案的路徑>>>>')
with open(r'test.txt', 'r', encoding='utf8') as test1:
ste = test1.read()
with open(file_path, 'a', encoding='utf8') as test2:
test2.write(ste)
2.結合檔案編寫使用者註冊登入功能
# 提前先建立一個空的userinfo.txt
使用者註冊 資料儲存到檔案中
使用者登入 資料來源於檔案
1.必須給我寫出來
單使用者註冊登入
2.拔高練習
多使用者註冊登入
while True:
print('1.註冊 2.登入')
inp = input('輸入編號選擇功能')
if inp == '1':
username_inp = input('請輸入使用者名稱>>>')
password_inp = input('請輸入密碼>>>')
user_str = username_inp + '|' + password_inp + '\n'
with open(r'userinfo.txt', 'a', encoding='utf8') as test:
test.write(user_str)
if inp == '2':
username_inp = input('請輸入使用者名稱>>>')
password_inp = input('請輸入密碼>>>')
user_str = username_inp + '|' + password_inp + '\n'
with open(r'userinfo.txt', 'r', encoding='utf8') as test:
for i in test:
if i == user_str:
print('登入成功')
break
else:
print('登入失敗')