The os.chmod(path, mode)
method is used to change permissions on files or directories. Read access to a directory means that you can get a list of file names in the directory. Execute permission means you can switch the working directory to this directory. Must have both write and execute permissions to delete or add files in the directory.
os.chmod Syntax
The syntax of the os.chmod(path, mode)
method is as follows:
os.chmod(path, mode)
os.chmod Parameters
path
– File name path or directory path.flags
– flags can be generated by bit or operation according to the following options.- stat.S_IXOTH: Execute by others(0o001)
- stat.S_IWOTH: Write by others(0o002)
- stat.S_IROTH: Set given file read by others.(0o004)
- stat.S_IRWXO: Read, write, and execute by others.(0o007)
- stat.S_IXGRP: Execute by group.(0o010)
- stat.S_IWGRP: Write by group.(0o020)
- stat.S_IRGRP: Read by group.(0o040)
- stat.S_IRWXG: Read, write, and execute by group.(0o070)
- stat.S_IXUSR: Set given file executed by the owner.(0o100)
- stat.S_IWUSR: Write by owner.(0o200)
- stat.S_IRUSR: Read by owner.(0o400)
- stat.S_IRWXU: Read, write, and execute by owner.(0o700)
- stat.S_ISVTX: sticky bit.(0o1000)
- stat.S_ISGID: set GID bit.(0o2000)
- stat.S_ISUID: set UID bit.(0o4000)
- stat.S_IREAD: Set given file read by the owner.(0o0400)
- stat.S_IWRITE: Set given file written by the owner.(0o0200)
os.chmod Returns
The python os.chmod(path, mode)
function doesn’t return any value.
os.chmod Demo #1: Basic usage of os.chmod
The following example demonstrates the use of the os.chmod(path, mode)
method −
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import stat
# Assuming the C:\Users\hp\Desktop\apimemos\apidemos.com.txt file exists
# set the file to be executable by the user group
os.chmod(r"C:\Users\hp\Desktop\apimemos\apidemos.com.txt", stat.S_IXGRP)
# Set the file to be writable by other users
os.chmod(r"C:\Users\hp\Desktop\apimemos\apidemos.com.txt", stat.S_IWOTH)
print("Use os.chmod to change mode successfully!!")
When we run above program, it produces following result −
Use os.chmod to change mode successfully!!