Open In App

Python | os.sysconf() method

Last Updated : 11 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

os.sysconf() method in Python is used to get integer-valued system configuration values. It accepts a string parameter name which specifies which configuration value is to retrieve. 

All possible values for name parameter is given as the keys of sysconf_names dictionary. We can also pass an integer value for name parameter for those configuration variables which is not included in the dictionary.

If the configuration variables specified by the name parameter is not defined by the system, then os.sysconf() method will return None and if the name does not specify any existing configuration variables then ValueError exception is raised. Also, if configuration value is not supported by the host operating system, OSError exception is raised. 

Note: os.sysconf() method is available only on UNIX platforms. 

Syntax: os.sysconf(name)
Parameter: 
name: A string or an integer value representing a system configuration variable.
Return Type: This method returns an integer value which represents the configuration value corresponding to the specified configuration variable. 
 

Code: Use of os.sysconf() method  

Python3




# 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 configuration 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 configuration 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 configuration value
print("Configuration value corresponding to % s :" % name, value)
  
# Note: -1 is returned if the
# configuration variable is not defined
# by the system 


Output: 

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

 

Code #2: Possible errors while using os.sysconf() method 

Python3




# 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.


Output: 

Traceback (most recent call last):
  File "sysconf.py", line 15, in 
    value = os.sysconf(name)
ValueError: unrecognized configuration name

 

Code #3: Handling possible errors while using os.sysconf() method 

Python3




# 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) 


Output: 

'PAGE_SIZE' is not a configuration variable

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads