Home >Backend Development >Python Tutorial >What is the python os.chmod() method? What role does it play?

What is the python os.chmod() method? What role does it play?

乌拉乌拉~
乌拉乌拉~Original
2018-08-17 14:45:212089browse

Today in this article we will learn about the python os.chmod() method. In the next article we will introduce the chmod method in python and introduce it. Definition and its functions and uses.

Overview

The os.chmod() method is used to change the permissions of a file or directory

Syntax

# The syntax format of the ##chmod() method is as follows:

os.chmod(path, mode)

Parameters

path -- file name path or directory path.

flags -- can be generated by bitwise OR operation with the following options. The read permission of the directory means that the list of file names in the directory can be obtained. The execution permission means that the working directory can be switched to this directory and files in the added directory can be deleted. Must have both write and execute permissions. File permissions are checked in the order of user id->group id->others, and the first matching allowed or prohibited permission is applied.

stat.S_IXOTH: Other users have execution rights 0o001

stat.S_IWOTH: Other users have write permissions 0o002

stat.S_IROTH: Other users have read permissions 0o004

stat.S_IRWXO: Other users have full permissions (permission mask) 0o007

stat.S_IXGRP: Group users have execute permissions 0o010

stat.S_IWGRP: Group users have write permissions 0o020

stat.S_IRGRP: Group user has read permission 0o040

stat.S_IRWXG: Group user has full permission (permission mask) 0o070

stat.S_IXUSR: Owner Has execution permission 0o100

stat.S_IWUSR: The owner has write permission 0o200

stat.S_IRUSR: The owner has read permission 0o400

stat.S_IRWXU: The owner has all Permissions (permission mask) 0o700

stat.S_ISVTX: Only the owner of the file directory in the directory can delete and change 0o1000

stat.S_ISGID: The effective group of the process that executes this file is the group where the file is located. 0o2000

stat.S_ISUID: The effective user of the process executing this file is the file owner 0o4000

stat.S_IREAD: Set to read-only under windows

stat.S_IWRITE: windows Cancel read-only

(This method has no return value.)

Example

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys, stat
# 假定 /tmp/foo.txt 文件存在,设置文件可以通过用户组执行
os.chmod("/tmp/foo.txt", stat.S_IXGRP)
# 设置文件可以被其他用户写入
os.chmod("/tmp/foo.txt", stat.S_IWOTH)
print "修改成功!!"

The output result of executing the above program is:

修改成功!!

The above is all the content of this article. I hope what I said and the examples I gave can be helpful to you.

For more related knowledge, please visit the

Python tutorial column on the php Chinese website.

The above is the detailed content of What is the python os.chmod() method? What role does it play?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more