Python小练习1:.txt文件常用读写操作
.txt文件常用讀寫操作
???? 本文通過一個實例來介紹讀寫txt文件的各種常用操作,問題修改自coursera上南京大學(xué)的課程:用Python玩轉(zhuǎn)數(shù)據(jù)。
???? 直接進入正題,考慮下面為練習(xí)讀寫txt文件的各種操作而設(shè)計的一個具體問題
???? 問題如下:
???? (1) 在任意位置創(chuàng)建一個.py文件,如'D:/編程練習(xí)/python練習(xí)/Week2_02.py'
???? (2) 在D盤下創(chuàng)建一個文件Blowing in the wind.txt,即‘D:\Blowing in the wind.txt’
???? 其內(nèi)容是:
How many roads must a man walk down
Before they call him a man
How many seas must a white dove sail
Before she sleeps in the sand
How many times must the cannon balls fly
Before they're forever banned
The answer my friend is blowing in the wind
The answer is blowing in the wind
???? (3) 在文件頭部插入歌名“Blowin’ in the wind”
?????(4) 在歌名后插入歌手名“Bob Dylan”
???? (5) 在文件末尾加上字符串“1962 by Warner Bros. Inc.”
?????(6) 在屏幕上打印文件內(nèi)容,最好加上自己的設(shè)計
?????(7) 以上每一個要求均作為一個獨立的步驟進行,即每次都重新打開并操作文件
--------------------------------------------------------------------------------------------------------------------
???? 程序代碼如下:
import os #Python的os模塊提供了執(zhí)行文件和目錄處理操作的函數(shù),例如重命名和刪除文件。 os.chdir('D:\\') #更改目錄 #------------------------------------------------------------------------- #創(chuàng)建一個文件,將歌詞寫入 f1=open(r'Blowing in the wind.txt','w') f1.write('How many roads must a man walk down \n') f1.write('Before they call him a man \n') f1.write('How many seas must a white dove sail \n') f1.write('Before she sleeps in the sand \n') f1.write('How many times must the cannon balls fly \n') f1.write('Before they\'re forever banned \n') f1.write('The answer my friend is blowing in the wind \n') f1.write('The answer is blowing in the wind\n') f1.close()#文件使用后記得關(guān)閉#-------------------------------------------------------------------------- #在文件頭部插入歌曲名 f2=open(r'Blowing in the wind.txt','r+')#mode參數(shù)不能用'w+',這會清空原內(nèi)容 lyrics =f2.readlines() lyrics.insert(0,'Blowin\'in the wind\n')#在第一行添加歌曲名 f2.seek(0,0)#將文件指針移動到文件開頭處 f2.writelines(lyrics) f2.close()#這是一種錯誤的寫法,將歌詞的第一行抹去了一部分 #f2=open(r'Blowing in the wind.txt','r+') #f2.seek(0,0)#將文件指針移動到文件開頭處 #f2.write('Blowin’ in the wind\n') #f2.close()#-------------------------------------------------------------------------- #在歌名后插入歌手名(實現(xiàn)同上一步) f3=open(r'Blowing in the wind.txt','r+')#mode參數(shù)不能用'w+',這會清空原內(nèi)容 lyrics =f3.readlines() lyrics.insert(1,'——Bob Dylan\n')#在第一行添加歌手名 f3.seek(0,0)#將文件指針移動到文件開頭處 f3.writelines(lyrics) f3.close()#-------------------------------------------------------------------------- #在文件末尾加上字符串“1962 by Warner Bros. Inc.” f4=open(r'Blowing in the wind.txt','a')#mode參數(shù)選擇'a',代表追加模式. f4.write('1962 by Warner Bros. Inc.')#追加模式下,可以直接向文件末尾write內(nèi)容 f4.close()#-------------------------------------------------------------------------- #在屏幕上打印文件內(nèi)容(最好加一些自己的格式設(shè)計) f5=open(r'Blowing in the wind.txt','r') article =f5.readlines()#讀取文件內(nèi)容 #按照自己的方式顯示 for i in range(0,len(article)):if 1<i<len(article)-1:print('\t'+article[i])else:print('\t\t'+article[i]) f5.close()
???? 運行后.txt文件中的內(nèi)容為:
???? 屏幕上的輸出為:
總結(jié)
以上是生活随笔為你收集整理的Python小练习1:.txt文件常用读写操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL自动建立集合自动分片_1.mo
- 下一篇: python依赖平台吗_在Python中