Latest web development tutorials

Python os.chmod () method

Python File (File) method Python OS file / directory methods


Outline

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

grammar

chmod () method syntax is as follows:

os.chmod(path, mode)

parameter

  • path - the path name of the file or directory path.

  • flags - available in the following options bitwise OR operation to generate, read permission on the directory means that you can obtain the directory list of file names, execute permission means that you can switch the working directory to this directory, delete the added files in the directory must have write and execute permissions, file permissions to user id-> group id-> other sequential test, the first match of permitted or prohibited privileges are applied.

    • stat.S_IXOTH: Other users have executive powers 0o001
    • stat.S_IWOTH: another user has write access 0o002
    • stat.S_IROTH: other users have read access 0o004
    • stat.S_IRWXO: Other users have full access to (permission mask) 0o007
    • stat.S_IXGRP: User Group Executive authority 0o010
    • stat.S_IWGRP: group write permissions 0o020
    • stat.S_IRGRP: User group read access 0o040
    • stat.S_IRWXG: group of users have full access to (permission mask) 0o070
    • stat.S_IXUSR: Owner has execute permissions 0o100
    • stat.S_IWUSR: Owner has write permissions 0o200
    • stat.S_IRUSR: the owner has read permission 0o400
    • stat.S_IRWXU: owner has full permissions (permission mask) 0o700
    • stat.S_ISVTX: directory file directory Only the owner can delete change 0o1000
    • stat.S_ISGID: the effective implementation of this document its process group file group 0o2000
    • stat.S_ISUID: the effective implementation of this document its process for the user owner of the file 0o4000
    • stat.S_IREAD: windows under the read-only
    • Cancel the read-only underwindows: stat.S_IWRITE

return value

This method has no return value.

Examples

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

#!/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 above program output is:

修改成功!!

Python File (File) method Python OS file / directory methods