Python中的OS模块提供了与操作系统进行交互的函数。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的函数的便携式方法。
os.sysconf()
Python中的方法用于获取integer-valued系统配置值。它接受一个字符串参数名称,该名称指定要检索的配置值。名称参数的所有可能值均作为sysconf_names词典的键给出。我们还可以为字典中未包含的那些配置变量的名称参数传递一个整数值。
如果系统未定义name参数指定的配置变量,则os.sysconf()
方法将返回没有如果名称未指定任何现有配置变量,则价值错误引发异常。另外,如果主机操作系统不支持配置值,OSError引发异常。
注意: os.sysconf()
该方法仅在UNIX平台上可用。
用法: os.sysconf(name)
参数:
name:代表系统配置变量的字符串或整数值。
返回类型:此方法返回一个整数值,该整数值表示与指定的配置变量相对应的配置值。
代码:os.sysconf()方法的使用
# Python program to explain os.sysconf() method
# importing os module
import os
# System Configuration variable
name = "SC_PAGE_SIZE"
# Get the integer-valued
# configuration value corresponding
# to the specified configuration
# variable using os.sysconf() method
value = os.sysconf(name)
# Print the configuraton value
print("% s:" % name, value)
# System Configuration variable
name1 = "SC_INT_MIN"
name2 = "SC_INT_MAX"
# Get the integer-valued
# configuration value corresponding
# to the specified configuration
# variable using os.sysconf() method
value1 = os.sysconf(name1)
value2 = os.sysconf(name2)
# Print the configuraton value
print("% s:" % name1, value1)
print("% s:" % name2, value2)
# We can also pass an integer
# value for name parameter.
# integer value must be present in
# os.sysconf_names dictionary as value
# of any configuration variable
# for example
conf_var = "SC_INT_MIN"
name = os.sysconf_names[conf_var]
print("\nInteger value corresponding to % s:" % conf_var, name)
# Get the integer-valued
# configuration value corresponding
# to the specified integer value
# using os.sysconf() method
value = os.sysconf(name)
# Print the configuraton value
print("Configuration value corresponding to % s:" % name, value)
# Note:-1 is returned if the
# configuration variable is not defined
# by the system
输出:
SC_PAGE_SIZE:4096 SC_INT_MIN:-2147483648 SC_INT_MAX:2147483647 Integer value corresponding to SC_INT_MIN:105 Configuration value corresponding to 105:-2147483648
代码2:使用os.sysconf()方法时可能出现的错误
# Python program to explain os.sysconf() method
# importing os module
import os
# System Configuration variable
name = "PAGE_SIZE"
# If the specified name
# is not a configuration variable
# then ValueError Exception
# is raised
value = os.sysconf(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 "sysconf.py", line 15, in value = os.sysconf(name) ValueError:unrecognized configuration name
代码3:使用os.sysconf()方法处理可能的错误
# Python program to explain os.sysconf() method
# importing os module
import os
# System Configuration variable
name = "PAGE_SIZE"
# we can handle exception
# using try and except block
# Try getting the system
# configuration value corresponding
# to specified configuration variable
try :
value = os.sysconf(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)
输出:
'PAGE_SIZE' is not a configuration variable
相关用法
- Python next()用法及代码示例
- Python os.dup()用法及代码示例
- Python set()用法及代码示例
- Python Decimal max()用法及代码示例
- Python PIL ImageOps.fit()用法及代码示例
- Python os.rmdir()用法及代码示例
- Python sympy.det()用法及代码示例
- Python Decimal min()用法及代码示例
- Python os.readlink()用法及代码示例
- Python os.writev()用法及代码示例
- Python os.readv()用法及代码示例
- Python PIL RankFilter()用法及代码示例
- Python os.rename()用法及代码示例
- Python os.sendfile()用法及代码示例
注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Python | os.sysconf() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。