Python3 os.chflags() Method

Python3 OS 文件/目录方法Python3 OS File/Directory Methods


Overview

The os.chflags() method is used to set the flags of a path to numeric flags. Multiple flags can be combined using OR.

Only supported on Unix.

Syntax

chflags()The method syntax is as follows:

os.chflags(path, flags)

Parameters

  • path-- File name path or directory path.

  • flags-- Can be the following values:

    • stat.UF_NODUMP:Non-dump file
    • stat.UF_IMMUTABLE:File is read-only
    • stat.UF_APPEND:File can only be appended
    • stat.UF_NOUNLINK:File cannot be deleted
    • stat.UF_OPAQUE:Directory is opaque and needs to be viewed through a union stack
    • stat.SF_ARCHIVED:Archivable file (settable by superuser)
    • stat.SF_IMMUTABLE:File is read-only (settable by superuser)
    • stat.SF_APPEND:File can only be appended (settable by superuser)
    • stat.SF_NOUNLINK:File cannot be deleted (settable by superuser)
    • stat.SF_SNAPSHOT:Snapshot file (settable by superuser)

Return Value

This method has no return value.

Example

The following example demonstrates the use of the chflags() method:

Example

#!/usr/bin/python3

import os,stat

path = "/tmp/foo.txt"

# Set flags for the file so that it cannot be renamed or deleted
flags = stat.SF_NOUNLINK
retval = os.chflags( path, flags )
print ("Return value: %s" % retval)

The result of executing the above program is:

返回值: None

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions