日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python小练习1:.txt文件常用读写操作

發(fā)布時(shí)間:2024/7/23 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python小练习1:.txt文件常用读写操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

.txt文件常用讀寫操作


???? 本文通過一個(gè)實(shí)例來介紹讀寫txt文件的各種常用操作,問題修改自coursera上南京大學(xué)的課程:用Python玩轉(zhuǎn)數(shù)據(jù)。

???? 直接進(jìn)入正題,考慮下面為練習(xí)讀寫txt文件的各種操作而設(shè)計(jì)的一個(gè)具體問題

???? 問題如下:

???? (1) 在任意位置創(chuàng)建一個(gè).py文件,如'D:/編程練習(xí)/python練習(xí)/Week2_02.py'

???? (2) 在D盤下創(chuàng)建一個(gè)文件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è)計(jì)

?????(7) 以上每一個(gè)要求均作為一個(gè)獨(dú)立的步驟進(jìn)行,即每次都重新打開并操作文件

--------------------------------------------------------------------------------------------------------------------

???? 程序代碼如下:

import os #Python的os模塊提供了執(zhí)行文件和目錄處理操作的函數(shù),例如重命名和刪除文件。 os.chdir('D:\\') #更改目錄 #------------------------------------------------------------------------- #創(chuàng)建一個(gè)文件,將歌詞寫入 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+',這會(huì)清空原內(nèi)容 lyrics =f2.readlines() lyrics.insert(0,'Blowin\'in the wind\n')#在第一行添加歌曲名 f2.seek(0,0)#將文件指針移動(dòng)到文件開頭處 f2.writelines(lyrics) f2.close()#這是一種錯(cuò)誤的寫法,將歌詞的第一行抹去了一部分 #f2=open(r'Blowing in the wind.txt','r+') #f2.seek(0,0)#將文件指針移動(dòng)到文件開頭處 #f2.write('Blowin’ in the wind\n') #f2.close()#-------------------------------------------------------------------------- #在歌名后插入歌手名(實(shí)現(xiàn)同上一步) f3=open(r'Blowing in the wind.txt','r+')#mode參數(shù)不能用'w+',這會(huì)清空原內(nèi)容 lyrics =f3.readlines() lyrics.insert(1,'——Bob Dylan\n')#在第一行添加歌手名 f3.seek(0,0)#將文件指針移動(dòng)到文件開頭處 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è)計(jì)) 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()

???? 運(yùn)行后.txt文件中的內(nèi)容為:

???? 屏幕上的輸出為:


總結(jié)

以上是生活随笔為你收集整理的Python小练习1:.txt文件常用读写操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。