python學習day9 字元編碼和檔案處理
1.字元編碼
x='上'
#unicode的二進位制---------》編碼--------》gbk格式的二進位制
res=x.encode('gbk') #bytes 位元組型別
print(res,type(res))
m=res.decode('gbk')
print(m) #把gbk解碼成unicode,python列印自動轉換成字元
2.檔案處理
1.什麼是檔案
檔案是作業系統為應用程式或者使用者提供一種操作硬碟的虛擬單位
強調:
檔案是作業系統提供的虛擬單位
應用程式或者使用者對檔案的讀寫操作其實都是向作業系統傳送指令
2.為何要用檔案
檔案對應的硬碟空間,如果需要考慮永久儲存資料那必須使用檔案
3.如何用檔案
路徑問題
絕對路徑 優點:無論從哪裡執行,都能找到檔案
缺點:比較冗長
win: c:\a\b\c\d.txt
linux: /a/b/c/d.txt
相對路徑:相當於當前執行檔案所在資料夾 優點:比較簡潔
缺點:必須在執行資料夾中才能找到檔案
a/b/c/d.txt
(1)開啟檔案
f=open(r'D:\abc\d\e.txt',mode='rt',encoding='utf-8') 以utf-8字元編碼表解碼儲存在硬碟中的檔案開啟到記憶體中。python應用程式會申請記憶體空間,存放f,另外會向作業系統發出申請,將檔案讀取進記憶體,f返回一個檔案控制代碼
print(f)
(2)讀/寫檔案
data=f.read() # 一次性把檔案全部讀出
print (data)
(3)關閉檔案
f.close() # 向作業系統傳送指令,讓作業系統關閉開啟的檔案,回收作業系統資源
print(f)
f.read
上下文管理
with open('a.txt',mode='rt',encoding='utf-8') as f: #結束使用檔案後,會自動關閉檔案
data=f.read()
print(data)
3.檔案的開啟模式
基本介紹
控制檔案讀寫操作的模式
r(預設)
w
a
控制檔案讀寫內容的模式(不能單獨使用,必須與r,w,a連用)
t
b
詳細介紹
控制檔案讀寫操作的模式
r(預設):只讀模式,以該模式開啟檔案,當檔案不存在時則報錯,當檔案存在時檔案指標在檔案開頭
with open('a.txt',mode='rt',encoding='utf-8') as f:
data=f.read()
print(f.readable()) #True
print(f.writable()) #False
line=f.readline()
print(line,end='')
print(data,type(data))
for line in f:
print(line)
print(f.readlines())# 逐行讀入內容,儲存成列表檔案,每行儲存成一個元素
w:只寫模式,以該模式開啟檔案,當檔案不存在時建立一個空文件,當檔案存在時清空檔案內容,檔案指標在檔案開頭
with open('a.txt',mode='wt,encoding='utf-8') as f:
print(f.readable()) # False
print(f.writable) # True
在打開了檔案不關閉的情況下,連續的寫入,新寫的內容總是跟在老內容之後
f.write('abc\n')
lines=['aaaa\n','2222\n','3333\n']
for line in lines:
f.write(line) 列表的元素一個一個的寫入到檔案中
f.writelines(lines)
f.write('aaaa\nbbb\nccc\n')
a:只追加寫模式,以該模式開啟檔案,當檔案不存在時建立一個空文件,當檔案存在時不清空檔案內容,檔案指標跳到檔案末尾
with open('c.txt',mode='at',encoding='utf-8') as f:
f.write('你好\n')
f.write('dadad\n')
f.writelines([1,2,3]) # 會報錯,整型不能加在str後面
控制檔案讀寫內容的模式(不能單獨使用,必須與r,w,a連用)
t(預設):無論讀寫都是以字元為單位的,只能操作文字檔案,必須指定encoding引數
b:無論讀寫都是以字元為單位的,可以操作所有檔案,一定不能指定encoding引數
with open('a.txt',mode='rb') as f:
data=f.read()
print(data,type(data))# 直接輸出硬碟中儲存的二進位制數
print(data.decode('utd-8'))# 把二進位制按照utf-8編碼表解碼成unicode,列印記憶體中unicode直接解碼成字元
with open('01.mp4',mode='rb') as f:
data=f.read()
print(data,type(data))
with open('a.txt',mode='wb') as f: # 直接讀取二進位制
f.write('字元打哈收到空間阿斯頓'.encode('utf-8')) 把字元對應的unicode編碼成utf-8二進位制寫入檔案
拷貝檔案小程式
inp_path=input('請輸入要開啟的檔案路徑:').strip()
inp_path1=input('請輸入要寫入的檔案路徑').strip()
with open(inp_path,mode='rb') as read_f\
open(inp_path,mode='wb') as write_f:
data = read_f.read()
write_f.write(data)
瞭解
+:不能單獨使用,必須與rwa連用 r+,w+,a+
with open('a.txt','r+t',encoding='utf-8') as f:
print(f.readable()) 可讀
print(f.writable()) 也可寫
data=f.reanline() 單獨讀一行內容,預設從開頭讀
print(data)
f.write('六子') 寫在檔案最後
# with open('a.txt','r+t',encoding='utf-8') as f:
# # print(f.readable())
# # print(f.writable())
#
# f.seek(10) 調整檔案指標,單位是位元組,unicode用3個位元組表示一個漢字,修改第10個位元組,會亂碼
# # print(f.tell())
# f.write('你好啊')修改檔案,是直接從當前指標往後修改,覆蓋掉舊內容
4.檔案修改的兩種方式
1.以讀的方式開啟原始檔
2.將檔案內容一次性全讀入記憶體,在記憶體中完成修改
3.以寫的方式開啟原始檔,然後將修改的結果一次性寫入原始檔
總結:
優點:在檔案修改過程中硬碟只存在一份資料
缺點:浪費記憶體
with open('a.txt',mode='rt',encoding='utf-8') as f1:
msg=f1.read()
new_msg=msg.replace('alex','SB')
with open('a.txt',mode='wt',encoding='utf-8') as f2:
f2.write(new_msg)
方式二
1.以讀的方式開啟原始檔,以寫的方式開啟一個臨時檔案
2.讀取原始檔的一行內容到記憶體中,將修改的結果存入臨時檔案,迴圈直到全部修改完
3.刪除原始檔,將臨時檔案重新命名為原始檔名
優點:同一時間在記憶體中只有檔案的一行內容,更節省記憶體
缺點:在檔案修改過程中硬碟會存在兩份資料
with open('b.txt',mode='rt',encoding='utf-8') as read_f,\
open('.b.txt.swap',mode='wt',encoding='utf-8') as write_f:
for line in read_f:
write_f.write(line.replace('sb','alex'))
os.remove('b.txt')
os.rename('.b.txt.swap','b.txt')