先找出現在home 的路徑
import os
home=os.path.expanduser("~")
print(home)
==============Q1
開始一個檔案 在home的路徑 建一個檔案 dumu.txt 文字為
遠上寒山石徑斜,
白雲生處有人家。
停車坐愛楓林晚,
霜葉紅於二月花。
import os
home=os.path.expanduser("~")
print(home)
file1=open("/home/up/dumu.txt","a")
file1.write("杜枚 山行")
file1.write("\n遠上寒山石徑斜,")
file1.write("\n白雲生處有人家。")
file1.write("\n停車坐愛楓林晚,")
file1.write("\n霜葉紅於二月花。")
file1.close()
=====================
Q2:開啟檔案 並讀取檔案
import os
home=os.path.expanduser("~")
print(home)
file2=open("/home/up/dumu.txt","r")
data=file2.readlines()
print(data)
file2.close()
==========
Q3: 列印標題及指標在文件的位址
import os
home=os.path.expanduser("~")
print(home)
file3=open("/home/up/dumu.txt","r")
file3.seek(0) #移動到指定位址
title=file3.read(5) #到指定位址 讀五個字
title_address=file3.tell()
print("標題是{}".format(title))
print("指標在文件的位址是{}".format(title_address))
file3.close()
沒有留言:
張貼留言