新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
python保存文件的方法-創(chuàng)新互聯(lián)
創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!

這篇文章主要介紹python保存文件的方法,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
python保存文件的方法,需要的小伙伴可以學(xué)習(xí)一下。
讀文件
f = open('Test.txt') #打開文件
data = f.read() #讀取文件
print(data)
# oneLine = f.readline()
# print oneLine #讀取第一行
# lines = f.readlines() #把內(nèi)容按行讀取至一個list
# print lines
f.close() #關(guān)閉寫文件
f = open('output.txt','w') #output.txt - 文件名稱及格式 w - writing
#以這種模式打開文件,原來文件內(nèi)容會被新寫入的內(nèi)容覆蓋,如文件不存在會自動創(chuàng)建
f.write("I'm going to write a string")
out = open('output.txt','w')
out.write("I'm Fine \nNow is raining!")
out.close()從控制臺輸入并保存文件
out = open('out.txt','w')while True:
data = input('Please Input something(Enter q quit): ')
if data != 'q':
out.write(data + '\n')
else:
break從一個文件讀取寫入到另一個文件中
newFile = open('output.txt')
data = newFile.read()
out = open('out.txt','w')
out.write(data)
newFile.close()
out.close()以上是python保存文件的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道!
當(dāng)前題目:python保存文件的方法-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://www.dlmjj.cn/article/csopic.html


咨詢
建站咨詢
