新聞詳情
Python教程—— File(文件) 方法發(fā)表時(shí)間:2023-02-25 16:09 open() 方法 Python open() 方法用于打開一個(gè)文件,并返回文件對象。 在對文件進(jìn)行處理過程都需要使用到這個(gè)函數(shù),如果該文件無法被打開,會(huì)拋出 OSError。 注意:使用 open() 方法一定要保證關(guān)閉文件對象,即調(diào)用 close() 方法。 open() 函數(shù)常用形式是接收兩個(gè)參數(shù):文件名(file)和模式(mode)。 open(file, mode='r') 完整的語法格式為: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) 參數(shù)說明: file: 必需,文件路徑(相對或者絕對路徑)。 mode: 可選,文件打開模式 buffering: 設(shè)置緩沖 encoding: 一般使用utf8 errors: 報(bào)錯(cuò)級別 newline: 區(qū)分換行符 closefd: 傳入的file參數(shù)類型 opener: 設(shè)置自定義開啟器,開啟器的返回值必須是一個(gè)打開的文件描述符。 下一篇母瑩
文章分類:
信息技術(shù)-教學(xué)資源
|