Python語言程式設計基礎(7)—— 檔案和資料格式化
阿新 • • 發佈:2018-11-07
返回字串
file = input() #返回字串 fo = open(file,"r").read(6) print(fo)
返回列表形式
file = input() fo = open(file,"r") #print(fo) #返回列表形式 print(fo.readlines(-1))
file = input() fo = open(file,"r") for line in fo.readlines(): print(line.replace('\n',"")) fo.close()
file = input() fo= open(file,"r") for line in fo: print(line.replace('\n',"")) fo.close()
w+
開啟一個檔案用於讀寫。如果該檔案已存在則開啟檔案,並從開頭開始編輯,即原有內容會被刪除。如果該檔案不存在,建立新檔案。
fname = input() fo = open(fname,"w+") ls = ["TreeDream","love","sea"] fo.writelines(",".join(ls)) fo.seek(0) for line in fo:print(line) fo.close()