Python中的OS模塊提供了與操作係統進行交互的函數。操作係統屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作係統的函數的便攜式方法。
os.confstr()
Python中的方法用於獲取string-valued係統配置值。它通常接受一個字符串參數名稱,該名稱指定要檢索的配置值。名稱參數的所有可能值均作為confstr_names詞典的鍵給出。我們還可以為字典中未包含的那些配置變量的名稱參數傳遞一個整數值。
如果係統未定義name參數指定的配置變量,則os.confstr()
方法將返回沒有如果名稱未指定任何現有配置變量,則價值錯誤引發異常。另外,如果主機操作係統不支持配置值,OSError引發異常。
注意: os.confstr()
該方法僅在UNIX平台上可用。
用法: os.confstr(name)
參數:
name:代表係統配置變量的字符串或整數值。
返回類型:此方法返回一個字符串值,該字符串值表示對應於指定配置變量的配置值。
代碼:os.confstr()方法的使用
# 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 configuraton 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 configuraton 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 configuraton value
print("Configuration value corresponding to % s:" % name, value)
# Note:-1 is returned if the
# configuration variable is not defined
# by the system
輸出:
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()方法時可能出現的錯誤
# 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()方法處理可能的錯誤
# 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
相關用法
- 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.confstr() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。