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 技術手冊 第三版
檔案系統作業
|
341
然而,如果你只為 v3 編寫程式,你可以讓等效的程式碼片段更為簡單且清
楚,方法是只捕捉適用的
OSError
子類別:
try: os.some_os_function_or_other()
except FileNotFoundError as err:
print('Warning: file', err.filename, 'not foundcontinuing')
errno
也提供一個名為
errorcode
的字典:鍵值是錯誤代碼,而對應的名稱
則是錯誤名稱,它們是像
'ENOENT'
這樣的字串。顯示
errno.errorcode[err.
errno]
作為你對某個
OSError
實體
err
的診斷資訊的一部分,通常可以讓診
斷更為清楚,並讓專精於該特定平台的讀者更容易理解。
檔案系統作業
使用
os
模組,你能以各種方式操作檔案系統:建立、複製或刪除檔案與目
錄;比較檔案;以及檢視有關檔案與目錄的檔案系統資訊。本節記載你用
於這些用途的
os
模組的屬性與方法,並涵蓋作用在檔案系統上的某些相關
模組。
os 模組的路徑字串屬性
一個檔案(file)或目錄(directory)是由一個字串來識別,稱為它的
path
,其語法取決於所在平台。在類 Unix Windows 平台上,
Python 都接受路徑的 Unix 語法,以一個斜線(
/
)作為目錄的分隔符號
directory separator)。在非類 Unix 平台上,Python 也接受平台特定的路
徑語法。特別是,在
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