python代码 读写文件
生活随笔
收集整理的這篇文章主要介紹了
python代码 读写文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
python 訪問文件
寫入
with open('test.txt','w') as f:f.write("hello,world!!!")讀取
read(x) 按字符讀取,x默認為-1,表示全部內容
readline() 讀入一行
readlines() 以列表形式全部讀入
情形一:分行讀入,逐行處理
fo=open('test.txt','r',encoding='utf-8') for line in fo: # fo.read(x),讀取前x個字符print(line) fo.close()情形二:一次讀入,分行處理
with open('test.txt','r',encoding='utf-8') as fo:print(fo.read())總結
以上是生活随笔為你收集整理的python代码 读写文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【IoT】WiFi 模组主流供应商
- 下一篇: python+django医院固定资产设