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

Python 技術手冊 第三版

by lex Martelli, Anna Martelli Ravenscroft, Steve Holden
January 2018
Intermediate 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.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

利用 Python 进行数据分析(原书第2版)

利用 Python 进行数据分析(原书第2版)

Wes McKinney

Publisher Resources

ISBN: 9789864766819