Python| os.confstr() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.confstr()方法用于获取字符串值的系统配置值。它通常接受一个字符串参数名称,该名称指定要检索的配置值。
name参数的所有可能值都作为confstr_names字典的键给出。我们还可以为那些不包含在字典中的配置变量传递一个整数值作为名称参数。
如果name参数指定的配置变量不是系统定义的,则os.confstr()方法将返回None ,如果 name 未指定任何现有配置变量,则引发ValueError异常。此外,如果主机操作系统不支持配置值,则会引发OSError异常。
注意: os.confstr()方法仅在 UNIX 平台上可用。
Syntax: os.confstr(name)
Parameter:
name: A string or an integer value representing a system configuration variable.
Return Type: This method returns a string value which represents the configuration value corresponding to the specified configuration variable.
代码: os.confstr() 方法的使用
Python3
# Python program to explain os.confstr() method
# importing os module
import os
# System Configuration variable
name = "CS_GNU_LIBC_VERSION"
# Get the configuration value
# corresponding to the
# specified configuration
# variable using os.confstr() method
value = os.confstr(name)
# Print the configuration value
print("% s :" % name, value)
# System Configuration variable
name = "CS_PATH"
# Get the configuration value
# corresponding to the
# specified configuration
# variable using os.confstr() method
value = os.confstr(name)
# Print the configuration value
print("% s :" % name, value)
# We can also pass an integer
# value for name parameter.
# for example
conf_var = "CS_GNU_LIBC_VERSION"
name = os.confstr_names[conf_var]
print("\nInteger value corresponding to % s:" % conf_var, name)
# Get the configuration value
# corresponding to the
# specified integer value
# using os.confstr() method
value = os.confstr(name)
# Print the configuration value
print("Configuration value corresponding to % s :" % name, value)
# Note: -1 is returned if the
# configuration variable is not defined
# by the system
Python3
# Python program to explain os.confstr() method
# importing os module
import os
# System Configuration variable
name = "cs_PATH"
# If the specified name
# is not a configuration variable
# then ValueError Exception
# is raised
value = os.confstr(name)
print("% s:" % name, value)
# Similarly, if the a specific
# value for name parameter is
# not supported by host operating system
# then OSError exception
# is raised.
Python3
# Python program to explain os.confstr() method
# importing os module
import os
# System Configuration variable
name = "cs_PATH"
# we can handle exception
# using try and except block
# Try getting the system
# configuration value corresponding
# to specified configuration variable
try :
value = os.confstr(name)
print("% s:" % name, value)
# If the specified name is
# not a configuration variable
except ValueError :
print("'% s' is not a configuration variable" % name)
# If the specified name is
# not supported by the
# operating system
except OSError :
print("'% s' is not supported by Operating system" % name)
CS_GNU_LIBC_VERSION : glibc 2.26
CS_PATH : /bin:/usr/bin
Integer value corresponding to CS_GNU_LIBC_VERSION: 2
Configuration value corresponding to 2 : glibc 2.26
代码 #2:使用 os.confstr() 方法时可能出现的错误
Python3
# Python program to explain os.confstr() method
# importing os module
import os
# System Configuration variable
name = "cs_PATH"
# If the specified name
# is not a configuration variable
# then ValueError Exception
# is raised
value = os.confstr(name)
print("% s:" % name, value)
# Similarly, if the a specific
# value for name parameter is
# not supported by host operating system
# then OSError exception
# is raised.
Traceback (most recent call last):
File "confstr.py", line 15, in
value = os.confstr(name)
ValueError: unrecognized configuration name
代码 #3:在使用 os.confstr() 方法时处理可能的错误
Python3
# Python program to explain os.confstr() method
# importing os module
import os
# System Configuration variable
name = "cs_PATH"
# we can handle exception
# using try and except block
# Try getting the system
# configuration value corresponding
# to specified configuration variable
try :
value = os.confstr(name)
print("% s:" % name, value)
# If the specified name is
# not a configuration variable
except ValueError :
print("'% s' is not a configuration variable" % name)
# If the specified name is
# not supported by the
# operating system
except OSError :
print("'% s' is not supported by Operating system" % name)
'cs_PATH' is not a configuration variable