Python os.pathconf() Method



The Python os.pathconf() method returns system configuration information relevant to a named file. It is used when we need to check a system-level property.

If we pass an invalid configuration value to this method, the ValueError exception will be raised. The valid values are given in the pathconf_names dictionary.

Syntax

The syntax for Python os.pathconf() method is shown below −

os.pathconf(path, name)

Parameters

The Python os.pathconf() method accepts the following parameters −

  • path − It indicates the file path.

  • name − This parameter specifies the configuration value which needs to be retrieve. It may be a string which is the name of a defined system value. These names are specified in a number of standards (POSIX.1, Unix 95, Unix 98, and others).

Return Value

The Python os.pathconf() method returns information about system configuration of a file.

Example

All configuration values related to the host operating system are located in the dictionary named "pathconf_names". In the following example, we are displaying the configuration values.

import os, sys
print ("%s" % os.pathconf_names)

When we run above program, it produces following result −

{'PC_ALLOC_SIZE_MIN': 18, 'PC_ASYNC_IO': 10, 'PC_CHOWN_RESTRICTED': 6,
 'PC_FILESIZEBITS': 13, 'PC_LINK_MAX': 0, 'PC_MAX_CANON': 1,
 'PC_MAX_INPUT': 2, 'PC_NAME_MAX': 3, 'PC_NO_TRUNC': 7, 
 'PC_PATH_MAX': 4, 'PC_PIPE_BUF': 5, 'PC_PRIO_IO': 11, 
 'PC_REC_INCR_XFER_SIZE': 14, 'PC_REC_MAX_XFER_SIZE': 15, 
 'PC_REC_MIN_XFER_SIZE': 16, 'PC_REC_XFER_ALIGN': 17, 
 'PC_SOCK_MAXBUF': 12, 'PC_SYMLINK_MAX': 19, 'PC_SYNC_IO': 9, 
 'PC_VDISABLE': 8}

Example

The following example shows the usage of pathconf() method. Here, we are passing two configuration values namely "PC_NAME_MAX" and "PC_FILESIZEBITS" to pathconf(). This will print the file size in bits and maximum length of a filename.

import os, sys

# Retrieve maximum length of a filename
no = os.pathconf("atty.py", "PC_NAME_MAX")
print ("Maximum length of a filename :%d" % no)

# Retrieve file size
no = os.pathconf("atty.py", "PC_FILESIZEBITS") 
print ("file size in bits  :%d" % no)

When we run above program, it produces following result −

Maximum length of a filename :255
file size in bits  :64
python_files_io.htm
Advertisements