文件處理——python經(jīng)典實(shí)例
提問人:楊紫紅發(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寫數(shù)據(jù),在寫之前先清空文件內(nèi)容,a打開并附加內(nè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寫數(shù)據(jù),在寫之前先清空文件內(nèi)容,a打開并附加內(nèi)容.
·打開文件之后記得關(guān)閉
繼續(xù)查找其他問題的答案?
相關(guān)視頻回答
點(diǎn)擊加載更多評(píng)論>>