The os.chflag(path, flags)
method is used to set the path’s tags to numeric tags. Multiple tags can be combined using OR.
os.chflag(path, flags)
only supported under Unix.
os.chflags Syntax
The syntax of the os.chflag(path, flags)
method is as follows:
os.chflag(path, flags)
os.chflags Parameters
path
– File name path or directory path.flags
– Can be the following values:- stat.UF_NODUMP: Non-dumped files
- stat.UF_IMMUTABLE: File is read-only
- stat.UF_APPEND: File may only be appended to
- stat.UF_NOUNLINK: File cannot be deleted
- stat.UF_OPAQUE: Directory is opaque, view through a union stack
- stat.SF_ARCHIVED: File may be archived (super user can set)
- stat.SF_IMMUTABLE: The file is read-only (super user can set)
- stat.SF_APPEND: Files can only be appended (super user can set)
- stat.SF_NOUNLINK: File cannot be deleted (super user can set)
- stat.SF_SNAPSHOT: The snapshot file (super user can set)
os.chflags Returns
The python os.chflag(path, flags)
function doesn’t return any value.
os.chflags Demo #1: Basic usage of os.chflags
The following example demonstrates the use of the os.chflag(path, flags)
method −
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import stat
path = "/tmp/apidemos.com.txt"
# Set a tag for a file so that it cannot be renamed and deleted
flags = stat.SF_NOUNLINK
retval = os.chflags( path, flags)
print("return value: %s" % retval)
When we run above program, it produces following result −
return value: None
os.chflags Demo #2: Check if os has this attribute before executing the os.chflags method
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import stat
path = "/tmp/apidemos.com.txt"
# Set a tag for a file so that it cannot be renamed and deleted
flags = stat.SF_NOUNLINK
retval = None
if hasattr(os, "chflags"):
retval = os.chflags( path, flags)
else:
print('os has no chflags attribute.')
print("return value: %s" % retval)
When we run above program on windows, it produces following result −
os has no chflags attribute.
return value: None