Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

open() 與 io 模組

233 views

Published on

使用 open()函式
使用 stdin、stdout、stderr
認識檔案描述器
認識 io 模組

Published in: Technology
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://redirect.is/fyxsb0u } ......................................................................................................................... Download Full EPUB Ebook here { https://redirect.is/fyxsb0u } ......................................................................................................................... Download Full doc Ebook here { https://redirect.is/fyxsb0u } ......................................................................................................................... Download PDF EBOOK here { https://redirect.is/fyxsb0u } ......................................................................................................................... Download EPUB Ebook here { https://redirect.is/fyxsb0u } ......................................................................................................................... Download doc Ebook here { https://redirect.is/fyxsb0u } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • DOWNLOAD THE BOOK INTO AVAILABLE FORMAT (New Update) ......................................................................................................................... ......................................................................................................................... Download Full PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download Full doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download PDF EBOOK here { https://urlzs.com/UABbn } ......................................................................................................................... Download EPUB Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... Download doc Ebook here { https://urlzs.com/UABbn } ......................................................................................................................... ......................................................................................................................... ................................................................................................................................... eBook is an electronic version of a traditional print book THE can be read by using a personal computer or by using an eBook reader. (An eBook reader can be a software application for use on a computer such as Microsoft's free Reader application, or a book-sized computer THE is used solely as a reading device such as Nuvomedia's Rocket eBook.) Users can purchase an eBook on diskette or CD, but the most popular method of getting an eBook is to purchase a downloadable file of the eBook (or other reading material) from a Web site (such as Barnes and Noble) to be read from the user's computer or reading device. Generally, an eBook can be downloaded in five minutes or less ......................................................................................................................... .............. Browse by Genre Available eBOOK .............................................................................................................................. Art, Biography, Business, Chick Lit, Children's, Christian, Classics, Comics, Contemporary, CookBOOK, Manga, Memoir, Music, Mystery, Non Fiction, Paranormal, Philosophy, Poetry, Psychology, Religion, Romance, Science, Science Fiction, Self Help, Suspense, Spirituality, Sports, Thriller, Travel, Young Adult, Crime, EBOOK, Fantasy, Fiction, Graphic Novels, Historical Fiction, History, Horror, Humor And Comedy, ......................................................................................................................... ......................................................................................................................... .....BEST SELLER FOR EBOOK RECOMMEND............................................................. ......................................................................................................................... Blowout: Corrupted Democracy, Rogue State Russia, and the Richest, Most Destructive Industry on Earth,-- The Ride of a Lifetime: Lessons Learned from 15 Years as CEO of the Walt Disney Company,-- Call Sign Chaos: Learning to Lead,-- StrengthsFinder 2.0,-- Stillness Is the Key,-- She Said: Breaking the Sexual Harassment Story THE Helped Ignite a Movement,-- Atomic Habits: An Easy & Proven Way to Build Good Habits & Break Bad Ones,-- Everything Is Figureoutable,-- What It Takes: Lessons in the Pursuit of Excellence,-- Rich Dad Poor Dad: What the Rich Teach Their Kids About Money THE the Poor and Middle Class Do Not!,-- The Total Money Makeover: Classic Edition: A Proven Plan for Financial Fitness,-- Shut Up and Listen!: Hard Business Truths THE Will Help You Succeed, ......................................................................................................................... .........................................................................................................................
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

open() 與 io 模組

  1. 1. 8. open() 與 io 模組 • 學習目標 – 使用 open()函式 – 使用 stdin、stdout、 stderr – 認識檔案描述器 – 認識 io 模組 2
  2. 2. 使用 open() 函式 • 在最基本的需求上,只需要使用到它的前 兩個參數:file 與 mode 3
  3. 3. read()、write()、close() 4
  4. 4. read() 方法 • 在未指定引數的情況下,會讀取檔案全部 的內容 在指定整數引數的情況下,會讀取指定的 字元數或位元組數(依開啟模式是文字或 者二進位而定) 5
  5. 5. • 檔案開啟模式與後續進行的操作必須符合, 否則引發 UnsupportedOperation 例外 • 可以使用 readable() 方法測試是否可讀 取,使用 writable() 方法測試可否寫入 6
  6. 6. readline()、readlines()、 writelines() • 對於文字模式來說,預設是讀取到'n'、 'r'或'rn',都可以被判定為一行 • readline()或readlines()讀到的每一 行,換行字元都一律換為'n'。 • 對於二進位模式來說,行的判斷標準預設 是遇到b'n'這個bytes • 文字模式在寫入的情況下, 任何'n'都 會被置換為os.linesep 的值(Windows 就是'rn') 7
  7. 7. 8
  8. 8. • 逐行讀取檔案內容 9
  9. 9. • Python 的檔案讀取風格:讀取一個檔案最 好的方式,就是不要去 read! 10
  10. 10. • 在進行檔案讀寫時,tell() 方法可以告知 目前在檔案中的位移值,單位是位元組值, 檔案開頭的位移值是 0 • seek() 方法可以指定跳到哪個位移值 11
  11. 11. • 可以使用 seek() 來實現隨機存取 • 可以執行 flush() 方法,將緩衝內容出清 12
  12. 12. readinto() 13
  13. 13. • buffering 參數用來設置緩衝策略 • 預設的緩衝策略會試著自行決定緩衝大小 (通常會是 4096 或 8192 位元組) • 或者對互動文字檔案採用行緩衝(line buffering) • 如果 buffering 設定為0,表示關閉緩衝 • 設定為大於0 的整數值,表示指定緩衝的位 元組大小 14
  14. 14. • encoding 指定文字模式時的檔案編碼 • 預設採用 locale.getpreferredencoding() 的傳回值作為編碼 • 如果文字檔案採用的編碼不同,讀取時就 有可能會出現亂碼問題 15
  15. 15. • 在正確指定 encoding 為 'UTF-8' 的情 況下,就不會有問題了: 16
  16. 16. • errors 參數可指定發生編碼錯誤時,該如 何進行處理 • 在不設定的情況下,發生編碼錯誤時會引 發 ValueError 的子類別例外 17
  17. 17. • newlines 的指定值還可以是''、'n'、 'r'與'rn' • 如果指定了'',讀取到'n'、'r'或 'rn', 都可以被判定為一行, 而 readline()或readlines()讀到的每一 行,一律保留來源換行字元 • 如果設定為其他'n'、'r'或'rn', 那麼讀取後的換行字元就會是指定的字元 18
  18. 18. • 文字模式在寫入的預設情況下,任何'n' 都會被置換為os.linesep 的值 • 如果newline 設為''就保留原有的換行 字元,如果指定為其他值,就以指定的字 元進行置換 19
  19. 19. stdin、stdout、stderr • 在 sys 模組中有個 stdin 就代表著標準輸 入,而 stdout 就代表著標準輸出 • 行為上就像 open() 函式開啟的文字模式 檔案物件 20
  20. 20. • 對於標準輸出或輸出, 若想二進位模式讀 取或寫入, 可使用sys.stdin.buffer 或sys.stdout.buffer • 行為就像是以open()函式開啟的二進位模 式檔案物件 21
  21. 21. • 可以改變標準輸入或輸出的來源 22
  22. 22. • 將一個自行開啟的檔案物件,指定給 sys.stdout,就可以利用 print() 來寫 出資料至檔案 • 內建的 print() 函式本身,就有一個 file 參數可以達到這樣的需求 23
  23. 23. • 可以使用 >將程式執行時的標準輸出訊息, 導向至指定的檔案,或者使用 >> 附加訊息 • 標準錯誤的輸出不能使用 >或 >> 重新導向 至檔案 24
  24. 24. 認識檔案描述器 • open() 函式的 file 參數,除了接受字串 指定檔案的路徑外,還可指定檔案描述器 • 檔案描述器會是個整數值,對應至目前程 式已開啟的檔案 • 標準輸入通常使用檔案描述器 0,標準輸出 是 1,標準錯誤是 2,進一步開啟的檔案則 會是 3、4、5 等數字 25
  25. 25. • 對於檔案物件,可以使用 fileno() 方法 來取得檔案描述器 26
  26. 26. • 如果指定 open() 的第一個參數為0 會如何 呢? 27
  27. 27. • 若能取得一個對應至系統上已開啟的檔案 描述器,就有機會使用 open() 來包裹成 為檔案物件,以利用檔案物件的高階行為 28
  28. 28. • open() 函式的 closed 參數預設值是 True – 這表示若 open()時 file 指定了一個檔案 描述器,在檔案物件呼叫 close()方法而關 閉時,被指定的檔案描述器也會一併關閉 – 當指定為 False 時,就不會關閉被指定的檔 案描述器 29
  29. 29. • open() 還有個 opener 可以用來指定一 個函式,該函式必須有兩個參數 – 第一個參數會傳入 open() 函式被指定的檔 案路徑 – 第二個參數會是 open() 函式依 mode 而計 算出來的 flags 值,函式最後必須傳回一個 檔案描述器 –open() 函式基於該檔案描述器建立檔案物件, 以便進行檔案操作。 30
  30. 30. 31
  31. 31. 認識 io 模組 • open() 函式實際上是個工廠函式 • 若想進一步掌握檔案物件的操作,就得認 識 io 模組 32
  32. 32. • 檔案物件大致上分為三個主要類型 – 文字(Text)I/O – 二進位(Binary)I/O – 原始(Raw)I/O 33
  33. 33. 34
  34. 34. 35
  35. 35. • 原始 I/O 是無緩衝低階操作,少直接使用, 常作為文字 I/O 或二進位 I/O 底層操作 36
  36. 36. • TextIOWrapper 也是個包裹器,可以包 裹 BufferedIOBase,以便將二進位資料 依指定的文字編碼進行轉換 37
  37. 37. • 如果資料的讀取來源或寫入目的地,並不 是個檔案,而想要是記憶體中某個物件, 那麼可以使用 BytesIO 或 StringIO 38
  38. 38. • 通常使用 BytesIO 時,最後會使用 getvalue() 方法來取得寫入的資料 39
  39. 39. • 若想讀寫的是文字資料,可以使用 StringIO 40

×