位置:首頁 > 軟件操作教程 > 編程開發(fā) > Python > 問題詳情

文件處理——python經(jīng)典實例

提問人:楊紫紅發(fā)布時間:2020-11-26
對比Java,python的文本處理再次讓人感動
#! /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()

知識點:
·open的參數(shù):r表示讀,w寫數(shù)據(jù),在寫之前先清空文件內(nèi)容,a打開并附加內(nèi)容. 
·打開文件之后記得關(guān)閉

繼續(xù)查找其他問題的答案?

相關(guān)視頻回答
回復(fù)(0)
返回頂部