如何使用Python获取与打开的文件相关的系统配置信息?

您可以调用fpathconf(file_descriptor,name)函数来获取与打开的文件有关的系统配置信息。名称指定要检索的配置值;它可以是一个字符串,它是定义的系统值的名称;这些名称在许多标准中都有规定。请注意,此功能仅在Unix系统上可用。例如,

import os, sys
# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# Now 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
os.close( fd)

当我们运行上面的程序时,它产生以下结果:

Maximum number of links to the file. :127
 Maximum length of a filename :255