Python os.chflags() Method



The Python os.chflags() method is used to set the flags of path to the numeric flags. It is available only in Unix. The flags may take a combination (bitwise OR) of the various values as described below.

  • os.UF_NODUMP − Do not dump the file.

  • os.UF_IMMUTABLE − The file may not be changed.

  • os.UF_APPEND − The file may only be appended to.

  • os.UF_NOUNLINK − The file may not be renamed or deleted.

  • os.UF_OPAQUE − The directory is opaque when viewed through a union stack.

  • os.SF_ARCHIVED − The file may be archived.

  • os.SF_IMMUTABLE − The file may not be changed.

  • os.SF_APPEND − The file may only be appended to.

  • os.SF_NOUNLINK − The file may not be renamed or deleted.

  • os.SF_SNAPSHOT − The file is a snapshot file.

Note − This method is available Python version 2.6 onwards. Most of the flags are only changeable by super-user.

Syntax

Following is the syntax of Python os.chflags() method −

os.chflags(path, flags)

Parameters

  • path − This is complete path of the directory to be changed to a new location.

  • flags − The flags specified are formed by OR.

Return Value

This method does not return any value.

Example

The following example shows the usage of Python os.chflags() method. Here, SF_NOUNLINK is passed as the flag argument to the method. This flag is used to mark a file such that it cannot be deleted or renamed by the user.

import os
import stat
path = "/tmp/foo.txt"
# Set a flag so that file may not be renamed or deleted.
flags = os.SF_NOUNLINK
retval = os.chflags( path, flags)
print ("Return Value: %s" % retval)

When we run above program, it produces following result −

Return Value : None

Example

In here, UF_COMPRESSED is passed as the flag argument to the os.chflags() method. This flag is used to check whether the file stored on the system is in compressed form.

import os
import stat
path = "code.txt"
# Set a flag so that file is stored compressed.
flag = os.UF_COMPRESSED
retval = os.chflags( path, flag)
print ("Return Value: %s" % retval)

While executing the above code we get the following output −

Return Value : None
os_file_methods.htm
Advertisements