- 讀出檔案內容
- 讀取幾個字
- 只讀第一行
- 把資料轉成陣列後讀出
- 使用for 迴圈讀取資料
- for 迴圈的end="執行後要出現的文字也可以是空白"
讀出檔案內容
#全部讀出
fo=open('welcome.txt','r')
w1=fo.read()
print(w1)
fo.close()

讀取幾個字
fo=open('welcome.txt','r')
ss=fo.read(5)
print(ss)
fo.close()

只讀第一行
fo=open('welcome.txt','r')
w1=fo.readline()
print(w1)
fo.close()

把資料轉成陣列後讀出
#把資料轉成陣列後讀出
fo=open('welcome.txt','r')
w1=fo.readlines()
print(w1)
print(w1[3])
fo.close()

使用for 迴圈讀取資料
#使用for迴圈
fo=open('welcome.txt','r')
for LL in fo:
print(LL) #每一圈跑一行,跑到沒資料為止
fo.close()

for 迴圈的end="執行後要出現的文字也可以是空白"
#讀取多行 end="執行後要出現的文字也可以是空白"
fo=open('welcome.txt','r')
for LL in fo:
print(LL,end="-")
fo.close()

Yiru@Studio - 關於我 - 意如