今日內容
- 文件操作
內容詳細
1.打開文件
1.open("作業講解.py",mode="r",encoding="utf-8")
2.文件打開模式
-
r/w/a(只讀只寫字符串)
-
r+/w+/a+(可讀可寫)
-
rb/wb/ab(只讀只寫二進制)
-
字符串轉換成"二進制"
v="你好"
data=v.encode("utf-8")
-
二進制轉換成字符串
v=""
data=data.decode("utf-8")
-
-
r+b/w+b/a+b(可讀可寫二進制)
1.mode=r:
以只讀方式打開文件 / 不能寫 / 如果文件不存在 則報錯
#打開文件
file_object=open("作業講解.py",mode="r",encoding="utf-8")
#讀文件
content=file_object.read()
print(content)
#關閉文件
file_object.close()
# mode=r只能讀 不能寫
file_object.write("dhhd")#文件中寫入內容
file_object.close()#關閉文件 將內存中內容保存到存儲地
#io.UnsupportedOperation: not writable
2.w:
以只能寫方式 打開文件 /不能讀 / 寫之前內容先被清空 / 文件不存在時候會新建文件
#在寫之前先清空文件,一般用于新建文件
#打開文件
file_object=open("file_test.txt",mode="w",encoding="utf-8")
#寫內容
file_object.write("測試")
file_object.close()
#"測試"
#mode=w 不能讀
file_object=open("file_test.txt",mode="w",encoding="utf-8")
file_object.write("測試")
content=file_object.read()
print(content)
file_object.close()
#io.UnsupportedOperation: not writable
3.a:
以只能追加方式打開文件 / 文件不存在則新建文件 / 光標永遠在內容最后位置
#寫入:a,只追加不能讀 + 不存在則新建
file_object=open("file_test.txt",mode="a",encoding="utf-8")
file_object.write("你好")
file_object.close()
#測試你好
4.r+:
以可讀可寫方式打開文件
-
讀:默認光標從0開始讀,也可以通過seek調整光標位置,讀物完后會改變光標 位置,所以在寫之前需要確定光標位置
-
寫:從光標所在的位置開始寫,也可以通過seek調整光標位置, 容易覆蓋已存在的內容
-
Seek(2): 在寫之前改變光標位置 從第二個字節開始
#文件已經存在的文件內容::"今天要好好學習哦" file_object=open("file_test.txt",mode="r+",encoding="utf-8") content=file_object.read()#讀取后光標移動到文件最后一個位置 file_object.seek(3)#改變光標位置 從第三個字節后開始寫入 file_object.write("你好")#第三個字節后的兩個字符,(天要)將會被覆蓋 #今你好好好學習哦 file_object.close()
6.w+:
-
寫入時候文件內容先被清空,所以讀取時需要調整光標
file_object=open("file_test.txt",mode="w+",encoding="utf-8") #在寫入之前文件內容就被清空 content=file_object.read() print(content)#打印空 file_object.write("aaaa") file_object.seek(0)#在讀取之前需要調整光標位置 不然讀取不到值 content1=file_object.read() print(content1) file_object.close()
6.a+ :
在之前文件內容上進行追加,打開文件光標就在內容的最后位置
? 所以讀取的時候需要調整光標
file_object=open("file_test.txt",mode="a+",encoding="utf-8")
#在之前文件內容上進行追加,打開文件光標就在內容的最后位置
# 所以讀取的時候需要調整光標
file_object.seek(0)
content=file_object.read()#
print(content)#打印空
# file_object.write("aaaa")
# file_object.seek(0)#在讀取之前需要調整光標位置
# content1=file_object.read()
# print(content1)
file_object.close()
7.rb/wb/ab
-
字符串轉換成"二進制"encode
v="你好"
data=v.encode("utf-8")
-
二進制轉換成字符串decode
v="\xe6"
data=data.decode("utf-8")
#示例1 w f=open("a.txt",mode="w",encoding="utf-8") f.write("你好")#傳入字符串 #將“你好” 根據encoding指定的編碼轉換成二進制: #"你好" -->10001010 10001010 10001010 10001010 10001010 10001010 #將二進制寫入到文件中 f.close() #示例二 wb一般用于圖片/音頻/視頻/未知編碼 f=open("a.txt",mode="wb")# #1.wb:把要寫入的字符串以二進制方式寫入 data="我好困" con=data.encode("utf-8") #將寫入的字符串按照的utf-8編碼轉換成二進制 f.write(con) #將二進制內容寫入文件 f.close() #示例三rb f=open("a.txt",mode="rb")# #直接讀取的是二進制數據 data=f.read() data=f.read(1)#表示一個字節 f.close() print(data)#打印十六進制 new_data=data.decode("utf-8")#二進制轉換成字符串 print(new_data)
7.r+b/w+b/a+b(可讀可寫二進制)
2.操作
1.讀取:
-
read() :從文件當前位置(光標) 開始讀取整個文件
-
readline():每次讀取一行內容,讀取時占用內存較小,適合大文件讀取
-
readlines():讀取文件所有行(包括換行符,特殊字符會被\轉義后存儲)存放在一個列表中,每行作為一個元素
-
read(2):讀取兩個字符,當讀取模式以rb 時候 表示兩個字節
-
#1.去除空格 file_object=open("file_test.txt",mode="r",encoding="utf-8") for line in file_object: line=line.strip() #去除內容中每一行的換行,不然會讀取文本的換行符 并打印到屏幕 print(line) # aaaa # hfh # jdjf # jdjjd#2.read 讀取全部文件 data=file_object.read() print(data) print(type(data)) #aaaa #hfh #jdjf #jdjjd #<class 'str'>#3.readline()讀取一行 data=file_object.readline() print(type(data))#<class 'str'> while data:#while循環讀取 并記錄光標位置 直至讀完為止 print(data.strip()) data=file_object.readline() file_object.close() #你好 fh #jdjf #jdjjd#4.readlines()讀取所以行 ata=file_object.readlines()#讀取所以行 包括換行符 print(type(data))# <class 'list'> print(data)# ['你\\\\\\n好 fh\n', 'jdjf\n', 'jdjjd\n']#特殊字符\被轉義 for line in data: print(line) # 你\\\n好 fh #print("\n")被打印執行 # # jdjf # # jdjjd
2.write()
4.seek():調整光標位置
5.tell():獲取當前光標的位置
6.flush():強制將內存的內容刷到硬盤文件中
f=open()
while True:
val=input("請輸入:")
f.write(val)
f.flush()#如果不寫flush 文件將永遠不會關閉 所以內容將永遠在內存中 不會保存到硬盤
f.close()
7.關閉
f.close()
with open("a.txt",mode="r"encoding="utf-8") as v:
data=v.read()
#文件執行完畢后將自動關閉
8.文件修改:必須全部拿到內存 進行修改,再重新刷進內存
with open("a.txt",mode="r" encoding="utf-8") as v:
data=v.read()
new_data=data.replace()
with open("a.txt",mode="w" encoding="utf-8") as v:
data=v.write(new_data)
#大文件時候
f1=open("a.txt",mode="r",encoding="utf-8")
f2=open("b.txt",mode="w",encoding="utf-8")
for line in f1:
new_line=line.replace("aa","bb")
f2.write(new_line)
f1.close()
f2.close()
#3.6之后寫法
with open("a.txt",mode="r" encoding="utf-8") as f1,with open("b.txt",mode="w" encoding="utf-8") as f2:
for line in f1:
new_line=line.replace("aa","bb")
f2.write(new_line)
3.練習
- 練習1:請將user中的元素根據 _ 連接,并寫入 'a1.txt' 的文件
練習1:請將user中的元素根據 _ 連接,并寫入 'a1.txt' 的文件
user = ['alex','eric']
con="_".join(user)
file_object=open("gao.txt",mode="w",encoding="utf-8")
file_object.write(con)
file_object.close()
- 練習2:請將user中的元素根據 alex|123形式,寫入 'a1.txt' 的文件
#練習2:請將user中的元素根據 alex|123形式,并寫入 'a1.txt' 的文件
user = [
{'name':'alex','pwd':'123'}, # alex|123
{'name':'eric','pwd':'olbody'}, # eric|olbody
]
#方案一 讀取value值 然后join拼接 加上換行
# file_object=open("gao.txt",mode="w",encoding="utf-8")
# for ele in user:
# ul="|".join(ele.values())
# file_object.write(ul)
# ##alex|123eric|olbody #沒有換行
# # ul = ul + "\n"
# # file_object.write(ul)
# #添加回車
# # alex | 123
# # eric | olbody
# file_object.close()
#方案二 占位符實現模式輸出
# file_object=open("gao.txt",mode="w",encoding="utf-8")
#
# for ele in user:
# temp="%s|%s\n"%(ele['name'],ele["pwd"],)
# file_object.write(temp)
# file_object.close()
3:請將a1.txt中的文件讀取出來并添加到一個列表中
- 去掉文件內容中讀取出來的換行(\n)
- 方案一
# 練習3:請將a2.txt中的文件讀取出來并添加到一個列表中 ['alex|123','eric|olbody']
file_object=open("gao.txt",mode="r",encoding="utf-8")
data=file_object.read()
file_object.close()
ul=[]
ul.append(data)#data讀取出來的字符內容中,包含特殊字符\n 所以會直接放到列表中 ,若直接print(data)則會將”\n”字符串作為一個python語句執行來,所以看不到有\n字符串
print(ul)
print(data)

#去掉換行\n
#方案一 :讀取全部內容 然后按照換行來切割
file_object=open("gao.txt",mode="r",encoding="utf-8")
data=file_object.read()
file_object.close()
ul=[]#存放data內容
ul.append(data)
print(ul)#打印data內的全部內容 ['alex|123\neric|olbody\n']
con=data.strip()#去掉字符串兩邊的換行['alex|123\neric|olbody']
con=con.split("\n")#按照\n切割
list_con=[]
list_con.extend(con)
print(list_con)
D05C82F]1.png)
2.方案二
#方案二 :讀取全部內容 一行一行遍歷 添加到列表中
result=[]
file_object=open("gao.txt",mode="r",encoding="utf-8")
for line in file_object:
line=line.strip()#去掉每一行換行
result.append((line))
file_object.close()
print(result)
4.總結
-
文件操作
-
打開
-
模式
-
r/w/a(只讀只寫字符串)
-
r+/w+/a+(可讀可寫)
-
rb/wb/ab(只讀只寫二進制)
-
字符串轉換成"二進制"
v="你好"
data=v.encode("utf-8")
-
二進制轉換成字符串
v=""
data=data.decode("utf-8")
-
-
r+b/w+b/a+b(可讀可寫二進制)
-
-
操作
-
讀
- read():讀全部文件
- read(1):
- r:讀取一個字符
- rb:讀取一個字節
- readline():讀取一行
- readlines():一行一行讀取整個文件
-
write
-
seek():調整光標位置
-
tell():獲取當前光標的位置
-
flush():強制將內存的內容刷到硬盤文件中
f=open() while True: val=input("請輸入:") f.write(val) f.flush()#如果不寫flush 文件將永遠不會關閉 所以內容將永遠在內存中 不會保存到硬盤 f.close() -
關閉
f.close() -
文件修改:必須全部拿到內存 進行修改,再重新刷進內存
-
-
浙公網安備 33010602011771號