文件處理——python經(jīng)典實(shí)例
提問(wèn)人:楊紫紅發(fā)布時(shí)間:2020-11-26
對(duì)比Java,python的文本處理再次讓人感動(dòng)
#! /usr/bin/python
spath="D:/download/baa.txt"
f=open(spath,"w") # Opens file for writing.Creates this file doesn't exist.
f.write("First line 1.\n")
f.writelines("First line 2.")
f.close()
f=open(spath,"r") # Opens file for reading
for line in f:
print("每一行的數(shù)據(jù)是:%s"%line)
f.close()
知識(shí)點(diǎn):
·open的參數(shù):r表示讀,w寫(xiě)數(shù)據(jù),在寫(xiě)之前先清空文件內(nèi)容,a打開(kāi)并附加內(nèi)容.
·打開(kāi)文件之后記得關(guān)閉
#! /usr/bin/python
spath="D:/download/baa.txt"
f=open(spath,"w") # Opens file for writing.Creates this file doesn't exist.
f.write("First line 1.\n")
f.writelines("First line 2.")
f.close()
f=open(spath,"r") # Opens file for reading
for line in f:
print("每一行的數(shù)據(jù)是:%s"%line)
f.close()
知識(shí)點(diǎn):
·open的參數(shù):r表示讀,w寫(xiě)數(shù)據(jù),在寫(xiě)之前先清空文件內(nèi)容,a打開(kāi)并附加內(nèi)容.
·打開(kāi)文件之后記得關(guān)閉
繼續(xù)查找其他問(wèn)題的答案?
相關(guān)視頻回答
- 開(kāi)始學(xué)習(xí)
海報(bào)設(shè)計(jì)精品錄播課程2121次觀(guān)看
開(kāi)始學(xué)習(xí)字體設(shè)計(jì)精品錄播課程2121次觀(guān)看
開(kāi)始學(xué)習(xí)室內(nèi)設(shè)計(jì)精品錄播課程2121次觀(guān)看
開(kāi)始學(xué)習(xí)影視動(dòng)畫(huà)精品錄播課程2121次觀(guān)看
開(kāi)始學(xué)習(xí)UI設(shè)計(jì)精品錄播課程2121次觀(guān)看
開(kāi)始學(xué)習(xí)
點(diǎn)擊加載更多評(píng)論>>