讀:r,rb,r+,r+b 寫:w,wb,w+,w+b 加:a,ab,a+,a+b tell():獲取光標(biāo)的位置 seek:調(diào)整光標(biāo)的位置 flush:強(qiáng)制刷新 “”" f = open(“中國(guó).txt”,encoding = “utf-8”,mode = “r”) content = f.read() print(content) f.close() read(n)按照字符去讀 f = open(“中國(guó).txt”,encoding = “utf-8”,mode = “r”) content = f.read(4) print(content) f.close() readline()一行一行讀 f = open(“中國(guó).txt”,encoding = “utf-8”,mode = “r”) content = f.readline() print(content) f.close() readlines()返回一個(gè)列表,列表的每個(gè)元素就是每行 f = open(“中國(guó).txt”,encoding = “utf-8”,mode = “r”) content = f.readlines() print(content) f.close() for讀取 f = open(“中國(guó).txt”,encoding = “utf-8”,mode = “r”) for line in f: print(line) f.close( #rb操作非文本的文件 f = open(“1.jpg”, mode=“rb”) c = f.read() f.close() f1 = open(“2.jpg”, mode=“wb”) f1.write? f1.close() w 文件的讀 如果文件存在先清空原內(nèi)容,,再寫入新內(nèi)容 f = open(“小人本.txt”,encoding = “utf-8”,mode = “w”) f.write(“垃圾日本”) f.close() wb操作非文本 f = open(“1.jpg”, mode=“rb”) c = f.read() f.close() f1 = open(“2.jpg”, mode=“wb”) f1.write? f1.close() 文件的追加a,ab,a+,a+b,沒有文件,,創(chuàng)建文件,追加內(nèi)容,,有文件在源文件的最后面加入內(nèi)容 f = open(“荷花.txt”,encoding = “utf-8”,mode = “a”) f.write(’\n大農(nóng)送’) f.close() 文件的讀寫r+,必須先讀再加,,順尋不能錯(cuò)誤 f = open(“中國(guó).txt”,encoding = “utf-8”,mode = “r+”) content = f.read() print(content) f.write("\n我愛中國(guó)1111") f.close() tell()獲取光標(biāo)的位置,先讀再獲取,,單位:字節(jié) f = open(“中國(guó).txt”,encoding = “utf-8”,mode = “r+”) print(f.tell()) content = f.read() print(f.tell()) f.close() seek調(diào)整光標(biāo)的位置,tell獲取光標(biāo)的位置 f = open(“中國(guó).txt”,encoding = “utf-8”,mode = “r+”) f.seek(6) content = f.read() print(content) print(f.seek(60)) print(f.tell()) f.close() flush強(qiáng)制刷新 f = open(“中國(guó).txt”,encoding = “utf-8”,mode = “r+”) f.write(“adas”) f.flush() f.close() with open不需要關(guān)閉文件,,open需要否則會(huì)一直占用內(nèi)存 with open (“中國(guó).txt”,encoding = “utf-8”) as f : print(f.read()) with open (“中國(guó).txt”,encoding = “utf-8”) as f, open (“荷花”,encoding = “utf-8”,mode = “w”) as f1: print(f.read()) f1.write(“荷花真漂亮”) import os with open (“中國(guó).txt”,encoding = “utf-8”,mode = “r”) as f, open (“我愛中國(guó).txt”,encoding = “utf-8”,mode = “w”) as f1: content = f.read() content1 = content.replace(“中國(guó)”,“美女”) f1.write(content) os.remove(“中國(guó).txt”) os.renames(“我愛中國(guó).txt”,“中國(guó).txt”) import os with open (“中國(guó).txt”,encoding = “utf-8”,mode = “r+”) as f: content = f.read() content1 = content.replace(“中國(guó)”,“美女”) f.write(content1) print(content1) import os with open (“中國(guó).txt”,encoding = “utf-8”,mode = “r”) as f, open (“我愛中國(guó).txt”,encoding = “utf-8”,mode = “w”) as f1: for line in f: new_line = line.replace(“美女”,“中國(guó)”) f1.write(new_line) os.remove(“中國(guó).txt”) os.renames(“我愛中國(guó).txt”,“中國(guó).txt”) 有關(guān)清空的內(nèi)容 關(guān)閉文件,再次以w模式打開此文件時(shí),,才會(huì)清空原有內(nèi)容 with open (“中國(guó).txt”,encoding = “utf-8”,mode = “w”) as f: for i in range(10): f.write(“阿松大”) 文章知識(shí)點(diǎn)與官方知識(shí)檔案匹配,,可進(jìn)一步學(xué)習(xí)相關(guān)知識(shí) Python技能樹基礎(chǔ)語法常用內(nèi)置函數(shù)21660 人正在系統(tǒng)學(xué)習(xí)中 ———————————————— 版權(quán)聲明:本文為CSDN博主「健身的杰Ray」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明,。 原文鏈接:https://blog.csdn.net/Python_allthing/article/details/122724919 |
|