Python os.fpathconf() 方法



Python OS 模組的 os.fpathconf() 方法檢索與開啟的檔案描述符相關的配置資訊。當我們需要檢查系統級屬性時,可以使用此方法。

如果我們將無效的配置值傳遞給此方法,則會引發 ValueError 異常。

注意: Python os.fpathconf() 只能在 UNIX 平臺上執行。

語法

以下是 fpathconf() 方法的語法:-

os.fpathconf(fd, name)

引數

Python os.fpathconf() 方法接受以下引數:-

  • fd - 這是要返回系統配置資訊的檔案描述符。

  • name - 此引數指定要檢索的配置值。它可以是字串,即已定義系統值的名稱;這些名稱在許多標準中指定(POSIX.1、Unix 95、Unix 98 等)。

返回值

Python os.fpathconf() 方法返回對應於開啟檔案的系統配置資訊。

示例

名為“pathconf_names”的字典包含與主機作業系統相關的所有配置值。在以下示例中,我們顯示了配置值。

#!/usr/bin/python
import os, sys

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
print ("%s" % os.pathconf_names)

# Close opened file
os.close( fd)
print ("Closed the file successfully!!")

當我們執行上述程式時,它會產生以下結果:-

{'PC_ALLOC_SIZE_MIN': 18, 'PC_ASYNC_IO': 10, 'PC_CHOWN_RESTRICTED': 6, 
'PC_FILESIZEBITS': 13, 'PC_LINK_MAX': 0, 'PC_MAX_CANON': 1, 'PC_MAX_INPUT': 2, 
'PC_NAME_MAX': 3, 'PC_NO_TRUNC': 7, 'PC_PATH_MAX': 4, 'PC_PIPE_BUF': 5, 'PC_PRIO_IO': 11, 
'PC_REC_INCR_XFER_SIZE': 14, 'PC_REC_MAX_XFER_SIZE': 15, 'PC_REC_MIN_XFER_SIZE': 16, 
'PC_REC_XFER_ALIGN': 17, 'PC_SOCK_MAXBUF': 12, 'PC_SYMLINK_MAX': 19, 
'PC_SYNC_IO': 9, 'PC_VDISABLE': 8}

Closed the file successfully!!

示例

以下示例顯示了 fpathconf() 方法的用法。在這裡,我們傳遞了兩個配置值,即“PC_LINK_MAX”和“PC_NAME_MAX”到 fpathconf()。這將列印最大連結數和檔名最大長度。

#!/usr/bin/python
import os, sys

# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# get maximum number of links to the file.
no = os.fpathconf(fd, 'PC_LINK_MAX')
print ("Maximum number of links to the file. :%d" % no)

# Now get maximum length of a filename
no = os.fpathconf(fd, 'PC_NAME_MAX')
print ("Maximum length of a filename :%d" % no)

# Close opened file
os.close( fd)
print ("Closed the file successfully!!")

執行上述程式後,它會列印以下結果:-

Maximum number of links to the file. :65000
Maximum length of a filename :255
Closed the file successfully!!
python_files_io.htm
廣告