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 技術手冊 第三版
318
|
10 章 檔案與文字作業
flush f.flush()
請求
f
的緩衝區(buffer)被寫出到作業系統,如此系統所見的檔案才會
有與 Python 的程式碼所寫入的完全相同的內容。取決於平台和
f
底層檔
案(underlying file)的本質,
f.flush
有可能無法確保想要的效果。
isatty f.isatty()
f
的底層檔案是一個互動式終端(interactive terminal)就為
True
,否
則為
False
fileno f.fileno()
回傳一個整數,即
f
的檔案在作業系統層級的檔案描述器(file
descriptor)。檔案描述器涵蓋於後面的「os 模組的檔案與目錄函式」
中。
mode mode
f.mode
是一個唯讀屬性,它是創建
f
io.open
呼叫中使用的
mode
字串的
值。
name name
f.name
是一個唯讀屬性,它是創建
f
io.open
呼叫中使用的
file
字串的
值。
read f.read(size=-1)
v2 v3 中,當
f
開啟於二進位模式,
read
會從
f
的檔案讀取最多
size
個位元,並將它們回傳為一個位元組字串。如果檔案在讀取
size
個位元
組之前就結束了,那麼
read
會讀取並回傳少於
size
個位元組。當
size
0
read
會讀取並回傳一直到檔案尾端的所有位元組。當檔案目前的
位置在檔案的尾端,或是
size
等於
0
read
就會回傳一個空字串。在 v3
中,如果
f
開啟於文字模式,
size
就是字元數,而非位元組數,而
read
會回傳一個文字字串。 ...
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