iT邦幫忙

2021 iThome 鐵人賽

DAY 28
0
自我挑戰組

我以為我今年休息但怎麼還是來報名了Python入門挑戰30天系列 第 28

D28 - 彭彭的課程# Python 實體物件的建立與使用 - 下篇 - 實體方法 - Instance Method(2)

可惡今天抽的各種卷我都沒中獎
嗚嗚
希望下禮拜可以輪到我

好今天要來練習讀檔案的包裝程式
會讀這兩個

# file實體物件:包裝檔案讀取的程式
class file:
    # 初始化函示
    def __init__(self,name):
        self.name=name
        # 尚未開啟檔案:初期是none (python代表空)
        self.file=None
    # 實體方法
    def open(self):
        self.file=open(self.name,mode="r",encoding="utf-8")
    # 實體方法
    def read(self):
        return self.file.read()
# 讀取第一個檔案
f1=file("data1.txt")
f1.open()
data=f1.read()
print(data)
# 讀取第二個檔案
f2=file("data2.txt")
f2.open()
data=f2.read()
print(data)


上一篇
D27 - 彭彭的課程# Python 實體物件的建立與使用 - 下篇 - 實體方法 - Instance Method(1)
下一篇
D29 - 彭彭的課程# Python Pandas 資料分析 - 基礎教學
系列文
我以為我今年休息但怎麼還是來報名了Python入門挑戰30天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言