Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python os.chmod() method

Python os.chmod() method

Python File methods Python OS file/directory methods


Overview

The os.chmod() method changes the permissions of a file or directory.

Syntax

The **chmod()** method syntax is as follows:


os.chmod(path, mode)

Parameters

- **stat.S_IXOTH:** Others have execute permission 0o001 - **stat.S_IWOTH:** Others have write permission 0o002 - **stat.S_IROTH:** Others have read permission 0o004 - **stat.S_IRWXO:** Others have all permissions (permission mask) 0o007 - **stat.S_IXGRP:** The group has execute permission 0o010 - **stat.S_IWGRP:** The group has write permission 0o020 - **stat.S_IRGRP:** The group has read permission 0o040 - **stat.S_IRWXG:** The group has all permissions (permission mask) 0o070 - **stat.S_IXUSR:** The owner has execute 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 may delete or change files in the directory 0o1000 - **stat.S_ISGID:** Executing this file sets the process effective group to the file's group 0o2000 - **stat.S_ISUID:** Executing this file sets the process effective user to the file's owner 0o4000 - **stat.S_IREAD:** Set read-only on Windows - **stat.S_IWRITE:** Remove read-only on Windows

Return Value

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

# Assume /tmp/foo.txt exists, set the file to be executable by the group

os.chmod("/tmp/foo.txt", stat.S_IXGRP)

# Set file to be writable by other users
os.chmod("/tmp/foo.txt", stat.S_IWOTH)

print "修改成功!!"

The output of the above program is:

修改成功!!

Python File methods Python OS file/directory methods