IT培訓(xùn)網(wǎng)
IT在線學(xué)習(xí)
Python讀寫(xiě)文件時(shí)非常流行的操作。Python讀文件有3種方法:read()、readline()和readlines()。Python寫(xiě)文件有2種方法:write()和writelines()。
可以先編輯一下文件并寫(xiě)入一些信息。在文本文件“example.txt”中寫(xiě)入如下內(nèi)容:
- "Whenever you feel like criticizing anyone,"
- he told me,
- “just remember that all the people in this world
- haven’t had the advantages that you’ve had.
1、讀取文件
文件對(duì)象的read()方法用于讀取文件,當(dāng)不傳遞任何參數(shù)時(shí),read()方法將讀取整個(gè)文件:
- >>> with open('example.txt', 'r') as f: # 使用r模式
- ... print(f.read()) # 將讀取內(nèi)容打印出來(lái)
- ...
- "Whenever you feel like criticizing anyone,"
- he told me,
- “just remember that all the people in this world
- haven’t had the advantages that you’ve had.
read()方法也可以通過(guò)傳遞參數(shù)來(lái)指定讀取的字節(jié)數(shù):
- >>> with open('example.txt', 'r') as f:
- ... print(f.read(8)) # 讀取8個(gè)字節(jié)的數(shù)據(jù)
- ...
- "Wheneve
readline()方法用于讀取整行文本:
- >>> with open('example.txt', 'r') as f:
- ... print(f.readline()) # 僅讀取第一行
- ...
- "Whenever you feel like criticizing anyone,"
- >>> with open('example.txt', 'r') as f:
- ... for _ in range(3): # 讀取前三行文本
- ... print(f.readline())
- ...
- "Whenever you feel like criticizing anyone,"
- he told me,
- “just remember that all the people in this world
readline()方法同樣可以通過(guò)傳遞參數(shù)來(lái)指定讀取的字節(jié)數(shù):
- >>> with open('example.txt', 'r') as f:
- ... for _ in range(3):
- ... print(f.readline(6))
- ...
- "Whene
- ver yo
- u feel
readlines()方法用于讀取文件對(duì)象剩余的全部行,以列表的形式返回:
- >>> with open('example.txt', 'r') as f:
- ... print(f.readlines())
- ...
- ['"Whenever you feel like criticizing anyone," \n', 'he told me, \n', '“just remember that all the people in this world \n', 'haven’t had the advantages that you’ve had.']
- >>> with open('example.txt', 'r') as f:
- ... print(f.readline()) # 先使用readline()讀取一行
- ... print(f.readlines()) # 再使用readlines()讀取剩余的全部行
- ...
- "Whenever you feel like criticizing anyone,"
- ['he told me, \n', '“just remember that all the people in this world \n', 'haven’t had the advantages that you’ve had.']
2、寫(xiě)入文件
使用Python寫(xiě)入文件時(shí),需要以寫(xiě)“w”或附加“a”模式打開(kāi)文件。需要謹(jǐn)慎使用“w”模式,因?yàn)樗鼤?huì)覆蓋文件(如果文件已存在),該文件之前的所有數(shù)據(jù)都將被刪除。寫(xiě)入字符串或字節(jié)序列(對(duì)于二進(jìn)制文件)是使用write()方法實(shí)現(xiàn)的,返回寫(xiě)入文件的字符數(shù):
- >>> with open('example.txt', 'w') as f: # 使用'w'模式
- ... f.write('I love Python')
- ... f.write('Hello!')
- ...
- 13
- 6
打開(kāi)文本文件“example.txt”,會(huì)發(fā)現(xiàn)文件中的內(nèi)容已被覆蓋了,原先的所有數(shù)據(jù)都被刪除了,此時(shí)文件中的內(nèi)容是剛剛使用write()方法寫(xiě)入的。
writelines()方法用于一次性寫(xiě)入多行文件:
- >>> with open('example.txt', 'a') as f: # 使用'a'追加模式
- ... f.writelines(['Over and Over', 'DealBreaker'])
- ...
再次打開(kāi)文本文件“example.txt”,會(huì)發(fā)現(xiàn)文件中原先的數(shù)據(jù)沒(méi)有被刪除,使用write()方法寫(xiě)入的數(shù)據(jù)被追加到了原數(shù)據(jù)的末尾。
>>本文地址:http://hqfphsz.com/zhuanye/2020/51378.html
聲明:本站稿件版權(quán)均屬中公教育優(yōu)就業(yè)所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
1 您的年齡
2 您的學(xué)歷
3 您更想做哪個(gè)方向的工作?
07月15日Java
咨詢/試聽(tīng)07月15日Python+人工智能
咨詢/試聽(tīng)07月15日Web前端
咨詢/試聽(tīng)07月15日UI設(shè)計(jì)
咨詢/試聽(tīng)07月15日大數(shù)據(jù)
咨詢/試聽(tīng)07月15日Java
咨詢/試聽(tīng)07月15日Python+人工智能
咨詢/試聽(tīng)07月15日Web前端
咨詢/試聽(tīng)07月15日UI設(shè)計(jì)
咨詢/試聽(tīng)07月15日大數(shù)據(jù)
咨詢/試聽(tīng)