您好,登錄后才能下訂單哦!
本文實(shí)例講述了Python打開文件、文件讀寫操作、with方式、文件常用函數(shù)。分享給大家供大家參考,具體如下:
在python3中,打開文件的函數(shù)是:
open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True)
file--文件名
mode—打開模式,默認(rèn)只讀模式
buffering--如果buffering的值被設(shè)為0,就不會(huì)有寄存。如果buffering的值取1,訪問文件時(shí)會(huì)寄存行。如果將buffering的值設(shè)為大于1的整數(shù),表明了這就是的寄存區(qū)的緩沖大小。如果取負(fù)值,寄存區(qū)的緩沖大小則為系統(tǒng)默認(rèn)。
encoding—打開文件的編碼方式
r:只讀模式(默認(rèn))
w :只寫模式,如果文件不存在就創(chuàng)建,如果存在,寫入的數(shù)據(jù)會(huì)覆蓋原來的數(shù)據(jù)
b :二進(jìn)制模式
t :文本模式
+:可寫可讀模式
a:追加模式,如果文件存在則文件指針指向文件末尾(追加數(shù)據(jù)),如果不存在就創(chuàng)建
r+:讀追加模式,先讀,再追加
w+:寫讀模式,先寫,意味著原本內(nèi)容丟失,再讀。
print("r".center(50,'-')) f=open("file.txt",encoding="utf-8") print(f.read()) f.close() ----------------- 運(yùn)行結(jié)果: my sas aaa fsafsa 中文 中文 葫蘆娃
文件使用完畢后必須關(guān)閉: 文件指針.close()
讀取文件內(nèi)容如下:
print("r".center(50,'-')) f=open("file.txt",encoding="utf-8") print(f.read()) f.close() --------------------------- 運(yùn)行結(jié)果: my sas aaa fsafsa 中文 中文 葫蘆娃
print("r".center(50,'-')) f=open("file.txt",encoding="utf-8") print(f.readline()) f.close() ----------- 運(yùn)行結(jié)果: my
print("r".center(50,'-')) f=open("file.txt",encoding="utf-8") print(f.readlines()) f.close() #------------------------r------------------------- #運(yùn)行結(jié)果: ['my\n', 'sas\n', 'aaa\n', 'fsafsa\n', '中文\n', '中文\n', '葫蘆娃\n', '\n']
print("r".center(50,'-')) f=open("file.txt","r+",encoding="utf-8") # print(f.readline()) f.write("hello mike") f.close()
結(jié)果:
print("r".center(50,'-')) f=open("file.txt","r+",encoding="utf-8") print(f.readline()) f.write("hello mike") f.close()
新結(jié)果:
myfile=open("myfile1","wb") myfile.write(b"nnnnnn") myfile.write("my葫蘆娃".encode("utf-8")) myfile.close()
myfile=open("myfile1","wb") myfile.write(b"nnnnnn") myfile.writelines([b'1',b'2',b'3',b'4']) myfile.close()
myfile=open("myfile1","wb+") myfile.write(b"nnnnnn") myfile.seek(0) print(myfile.read()) myfile.close()
myfile=open("myfile1","wb+") myfile.write(b"1nnnnnn") site=myfile.tell() myfile.write(b"2nnnnnn") myfile.seek(site)##讀出后一段 print(myfile.read()) myfile.close()
with open("file.txt","r+",encoding="utf-8") as f:##將自動(dòng)執(zhí)行f.close() print(f.tell()) f.write("金剛") for line in f: print(line,end="")
with open("file.txt",'r') as f ,\ open("file.new",'r') as m: print(f.read(),m.read())
file.close():關(guān)閉文件。關(guān)閉后文件不能再進(jìn)行讀寫操作
file.seek(offset[, whence]):設(shè)置文件當(dāng)前位置
file.tell():返回文件當(dāng)前位置。
myfile=open("myfile1","wb+") myfile.write(b"1nnnnnn") site=myfile.tell() myfile.write(b"2nnnnnn") myfile.seek(site)##讀出后一段 print(myfile.read()) myfile.close()
file.flush():刷新文件內(nèi)部緩沖,立即把內(nèi)部緩沖區(qū)的數(shù)據(jù)寫入文件,因?yàn)椴⒉皇邱R上將文件
import time myfile=open("myfile1","wb+") myfile.write(b"1nnnnnn") time.sleep(10) # myfile.flush() myfile.write(b"2nnnnnn") myfile.close()
上述代碼,直到程序運(yùn)行完成才一次性寫入“1nnnnnn2nnnnnn”
import time myfile=open("myfile1","wb+") myfile.write(b"1nnnnnn") myfile.flush() time.sleep(10) myfile.write(b"2nnnnnn") myfile.close()
上述代碼,可以看到,在程序sleep之前就已經(jīng)寫入了“1nnnnnn”
file.truncate([size]):截取文件,從文件開頭,截到指定位置,會(huì)覆蓋原文件。
文件內(nèi)容:
print("r".center(50,'-')) f=open("file.txt","r+",encoding="utf-8") print(f.readline()) print("----truncate()-------") print(f.tell()) m=f.tell() f.truncate(m)#內(nèi)容從0位置截?cái)嗟街付ㄎ恢?,不論?dāng)前光標(biāo)位置 f.close()
執(zhí)行后,文件內(nèi)容:
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python文件與目錄操作技巧匯總》、《Python文本文件操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。