今天要跟大家介紹Python內建的函式open()
以下是官方文件的介紹:
Open file and return a corresponding file object. If the file cannot be opened, an OSError is raised.
描述
Python open() 函式用於打開一個文件,並回傳文件對象,如果該文件無法被打開,會提示 OSError。
語法
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
參數
- file--必需,文件路徑(相對或絕對路徑)
- mode: 可選,文件打開模式
- buffering: 設置緩衝
- encoding: 一般使用萬國編碼(utf8)
- errors: 報錯級別
- newline: 區分換行符號
- closefd: 傳入的file參數類型
mode參數
Character | Meaning |
---|---|
'r' | open for reading (default) |
'w' | open for writing, truncating the file first |
'x' | open for exclusive creation, failing if the file already exists |
'a' | open for writing, appending to the end of the file if it exists |
'b' | binary mode |
't' | text mode (default) |
'+' | open a disk file for updating (reading and writing) |
回傳值
該函式無回傳值 Orz