新聞中心
文件對象
These APIs are a minimal emulation of the python 2 C API for built-in file objects, which used to rely on the buffered I/O (FILE*) support from the C standard library. In Python 3, files and streams use the new io module, which defines several layers over the low-level unbuffered I/O of the operating system. The functions described below are convenience C wrappers over these new APIs, and meant mostly for internal error reporting in the interpreter; third-party code is advised to access the io APIs instead.

法庫網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,法庫網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為法庫成百上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的法庫做網(wǎng)站的公司定做!
PyObject *PyFile_FromFd(int fd, const char *name, const char *mode, int buffering, const char *encoding, const char *errors, const char *newline, int closefd)
Return value: New reference. Part of the Stable ABI.
根據(jù)已打開文件 fd 的文件描述符創(chuàng)建一個 Python 文件對象。 參數(shù) name, encoding, errors 和 newline 可以為 NULL 表示使用默認值;buffering 可以為 -1 表示使用默認值。 name 會被忽略僅保留用于向下兼容。 失敗時返回 NULL。 有關(guān)參數(shù)的更全面描述,請參閱 io.open() 函數(shù)的文檔。
警告
由于Python流具有自己的緩沖層,因此將它們與 OS 級文件描述符混合會產(chǎn)生各種問題(例如數(shù)據(jù)的意外排序)。
在 3.2 版更改: 忽略 name 屬性。
int PyObject_AsFileDescriptor(PyObject *p)
Part of the Stable ABI.
Return the file descriptor associated with p as an int. If the object is an integer, its value is returned. If not, the object’s fileno() method is called if it exists; the method must return an integer, which is returned as the file descriptor value. Sets an exception and returns -1 on failure.
PyObject *PyFile_GetLine(PyObject *p, int n)
Return value: New reference. Part of the Stable ABI.
等價于 p.readline([n]) ,這個函數(shù)從對象 p 中讀取一行。 p 可以是文件對象或具有 readline() 方法的任何對象。 如果 n 是 0 ,則無論該行的長度如何,都會讀取一行。 如果 n 大于``0``,則從文件中讀取不超過 n 個字節(jié);可以返回行的一部分。 在這兩種情況下,如果立即到達文件末尾,則返回空字符串。 但是,如果 n 小于 0 ,則無論長度如何都會讀取一行,但是如果立即到達文件末尾,則引發(fā) EOFError。
int PyFile_SetOpenCodeHook(Py_OpenCodeHookFunction handler)
重載 io.open_code() 的正常行為,將其形參通過所提供的處理程序來傳遞。
The handler is a function of type PyObject *(*)(PyObject *path, void *userData), where path is guaranteed to be PyUnicodeObject.
userData 指針會被傳入鉤子函數(shù)。 因于鉤子函數(shù)可能由不同的運行時調(diào)用,該指針不應(yīng)直接指向 Python 狀態(tài)。
鑒于這個鉤子專門在導(dǎo)入期間使用的,請避免在新模塊執(zhí)行期間進行導(dǎo)入操作,除非已知它們?yōu)閮鼋Y(jié)狀態(tài)或者是在 sys.modules 中可用。
一旦鉤子被設(shè)定,它就不能被移除或替換,之后對 PyFile_SetOpenCodeHook() 的調(diào)用也將失敗,如果解釋器已經(jīng)被初始化,函數(shù)將返回 -1 并設(shè)置一個異常。
此函數(shù)可以安全地在 Py_Initialize() 之前調(diào)用。
引發(fā)一個 審計事件 setopencodehook,不附帶任何參數(shù)。
3.8 新版功能.
int PyFile_WriteObject(PyObject *obj, PyObject *p, int flags)
Part of the Stable ABI.
將對象 obj 寫入文件對象 p 。 flags 唯一支持的標志是 Py_PRINT_RAW;如果給定,則寫入對象的 str() 而不是 repr()。成功時返回 0,失敗時返回 -1。 將設(shè)置適當?shù)睦狻?/p>
int PyFile_WriteString(const char *s, PyObject *p)
Part of the Stable ABI.
將字符串 s 寫入文件對象 p。 成功返回 0 失敗返回 -1;將設(shè)定相應(yīng)的異常。
分享名稱:創(chuàng)新互聯(lián)Python教程:文件對象
分享路徑:http://fisionsoft.com.cn/article/dpohipp.html


咨詢
建站咨詢
