Skip to Content
Python 技術手冊 第三版
book

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate to advanced content levelIntermediate to advanced
856 pages
17h 17m
Chinese
GoTop Information, Inc.
Content preview from Python 技術手冊 第三版
檔案 I/O 的輔助模組
|
323
檔案 I/O 的輔助模組
「檔案」物件提供檔案 I/O 所需的最底限功能性,而某些輔助用的 Python
程式庫模組則提供便利的補充功能性,在幾種重要的情況中,讓 I/O 變得
更加容易且方便。
fileinput 模組
fileinput
模組讓你以迴圈處理文字檔案所成的一個串列(a list of text
files)中的所有文字行。效能良好,堪比在每個檔案上直接迭代的效能,
因為用了緩衝來最小化 I/O。因此,只要你覺得該模組豐富的功能性很方
便,你就能將模組
fileinput
用於資料行導向(line-oriented)的輸入,而
不用擔心效能。
input
函式是模組
fileinput
的關鍵函式;該模組也提供一
FileInput
類別,其方法提供與該模組的函式相同的功能性。該模組的內
容列於此:
close close()
關閉整個序列,讓迭代停止,並且沒有檔案仍然開啟。
FileInput class FileInput(files=None, inplace=False, backup='', bufsize=0,
openhook=None)
創建並回傳類別
FileInput
的一個實體
f
。引數與
fileinput.input
相同,
f
的方法與模組
fileinput
的函式有相同的名稱、引數和語意。
f
也提
供方法
readline
,它會讀取並回傳下一行。當你想要內嵌或混合會從多個
檔案序列讀取資料行的迴圈,你就可以明確地使用類別
FileInput
filelineno ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

測試驅動開發|使用 Python

測試驅動開發|使用 Python

Harry J.W. Percival
Linux 内核观测技术BPF

Linux 内核观测技术BPF

David Calavera, Lorenzo Fontana

Publisher Resources

ISBN: 9789864766819