Python: Modify file permissions

Source: Internet
Author: User

OS. chmod () method this method updates the path or File Permission In numeric mode. This mode can take one of the following values or bitwise OR operation combinations:

  • Stat. s_isuid: Set User ID on execution.
  • Stat. s_isgid: Set Group ID on execution.
  • Stat. s_enfmt: record locking enforced.
  • Stat. s_isvtx: save text image after execution.
  • Stat. s_iread: Read by owner.
  • Stat. s_iwrite: write by owner.
  • Stat. s_iexec: Execute by owner.
  • Stat. s_irwxu: read, write, and execute by owner.
  • Stat. s_irusr: Read by owner.
  • Stat. s_iwusr: write by owner.
  • Stat. s_ixusr: Execute by owner.
  • Stat. s_irwxg: read, write, and execute by group.
  • Stat. s_irgrp: Read by group.
  • Stat. s_iwgrp: write by group.
  • Stat. s_ixgrp: Execute by group.
  • Stat. s_irwxo: read, write, and execute by others.
  • Stat. s_iroth: Read by others.
  • Stat. s_iwoth: write by others.
  • Stat. s_ixoth: Execute by others.

Syntax:

OS. chmod (path, mode );

Parameters:

  • Path: this is the path for which mode wocould be set.
  • Mode: this may take one of the above mentioned values or bitwise ored combinations of them:

Example 1:

#!/usr/bin/python
import os, sys, stat
# Assuming /tmp/foo.txt exists, Set a file execute by the group.
os.chmod("/tmp/foo.txt", stat.S_IXGRP)
# Set a file write by others.
os.chmod("/tmp/foo.txt", stat.S_IWOTH)
print "Changed mode successfully!!"
This produces following result:
Changed mode successfully!!
os.chmod("/tmp/foo.txt", stat.S_IRWXU|stat.S_IRGRP|stat.S_IROTH) # mode:777

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.