winter vacation project 2021

  • Home
    • Site Map
    • reveal
    • blog
  • W1
    • Day1(0116)
      • Python源起
      • 基本Python程式
      • Python程式碼格式重點
      • 常見程式設計錯誤(bug)
    • Day2(0117)
      • 型別
    • Day3(0118)
      • 變數
    • Day4(0119)
      • 常數
      • 運算子
    • Day5(0120)
      • 運算子個別解說(一)
    • Day6(0121)
      • 運算子個別解說(二)
    • Day7(0122)
      • 運算子的優先順序
      • 輸出
  • W2
    • Day8(0123)
      • 輸入
    • Day9(0124)
      • 常用內建數值函數
    • Day10(0125)
      • 導入數學模組
      • 常用數學函式
      • 亂數函式
    • Day11(0126)
      • 逸脫序列
      • 常用內建字串函式
      • 連接運算子
      • 重複運算子
      • in ,not in運算子
      • 索引運算子[]與片段運算子
    • Day12(0127)
      • 字串處理方法
      • 常用字串轉換方法
      • 搜尋字串方法
      • 常用字串測試方法
    • Day13(0128)
      • 字串格式化方法
      • 刪除指定字元方法
    • Day14(0129)
      • 數值與字串格式化
  • W3
    • Day15(0130)
      • 認識流程控制
      • if選擇結構
    • Day16(0131)
      • for迴圈(for loop)
      • 使用range物件作為迴圈迭代的物件
    • Day17(0201)
      • 使用list作為迴圈迭代的物件
      • 使用字串作為迴圈迭代的物件
      • 巢狀for迴圈
    • Day18(0202)
      • while迴圈
    • Day19(0203)
      • 認識函式
      • 定義函式
    • Day20(0204)
      • 呼叫函式
      • 函式參數
    • Day21(0205)
      • 函式的傳回值
      • 全域變數與區域變數
  • W4
    • Day22(0206)
      • 遞迴函式
      • lambda 運算式
      • 日期時間函式(一)
    • Day23(0207)
      • 日期時間函式(二)
      • calendar模組
    • Day24(0208)
      • list串列(一)
    • Day25(0209)
      • list串列(二)
    • Day26(0210)
      • list串列(三)
    • Day27(0211)
      • tuple序對
    • Day28(0212)
      • set集合(一)
  • W5
    • Day29(0213)
      • set集合(二)
    • Day30(0214)
      • dict字典(一)
    • Day31(0215)
      • dict字典(二)
    • Day32(0216)
      • turtle模組繪圖(一)
    • Day33(0217)
      • turtle模組繪圖(二)
    • Day34(0218)
      • 檔案存取(一)
    • Day35(0219)
      • 檔案存取(二)
  • W6
    • Day36(0220)
      • 檔案存取(三)
    • Day37(0221)
      • 例外處理(一)
    • Day38(0222)
      • 例外處理(二)
    • Day39(0223)
      • 物件導向(OO)
    • Day40(0224)
      • 物件導向程式設計(OPP)
    • Day41(0225)
      • 使用類別與物件
    • Day42(0226)
      • 定義類別
  • 資料參考
Day36(0220) << Previous Next >> Day37(0221)

檔案存取(三)

使用readline()方法從檔案讀取資料

可利用使用readline()方法從檔案讀取一行資料,然後傳回該字串,若傳回空字串,表示抵達檔案結尾,實例如下:

fileObject = open("test1.txt","r")

line = fileObject.readline()
while line != '':
        print(line)
        line = fileObject.readline()

fileObject.close()

使用readlines()方法從檔案讀取資料

>>> fileObject = open("test.txt","r")

>>> content = fileObject.readline()
>>> print(content)
['登鸛雀樓\n','白日依山盡,黃河入海流。\n','欲窮千里目,更上一層樓。']
>>> for line in content:
               print(line)

登鸛雀樓
白日依山盡,黃河入海流。
欲窮千里目,更上一層樓。

>>> fileObject.close()
>>>

檢查檔案是否存在

可利用os.path模組提供的isfile(file)函式檢查參數file所指定的檔案是否存在,是就傳回True,否則傳回False。實例如下:

#匯入 os.path 模組
import os.path

#檢查檔案是否存在,是就讀取所有內容,否則印出「此檔案不存在」
if os.path.isfile("test.txt"):
        fileObject = open("test.txt","r")
        for line in fileObject:
                print(line)
        fileObject.close()
else:
        print("此檔案不存在")

with敘述

利用with敘述可自動關閉檔案無須額外呼叫close(),語法如下:

with open(file,mode) as 檔案物件名稱:
        ......         #存取檔案的動作

Day36(0220) << Previous Next >> Day37(0221)

Copyright © All rights reserved | This template is made with by Colorlib