Python to view the read and write permissions of files, python to read and write

Source: Internet
Author: User

Python to view the read and write permissions of files, python to read and write

Example:

# -*- coding: utf-8 -*-# @author flynetcnimport sys, os, pwd, stat, datetime;LOG_FILE = '/var/log/checkDirPermission.log';nginxWritableDirs = ['/var/log/nginx','/usr/local/www/var',];otherReadableDirs = ['/var/log/nginx','/usr/local/www/var/log',];dirs = [];files = [];def logger(level, str):logFd = open(LOG_FILE, 'a');logFd.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')+": "+("WARNING " if level else "NOTICE ")+str);logFd.close();def walktree(top, callback):for f in os.listdir(top):pathname = os.path.join(top, f);mode = os.stat(pathname).st_mode;if stat.S_ISDIR(mode):callback(pathname, True);walktree(pathname, callback);elif stat.S_ISREG(mode):callback(pathname, False);else:logger(1, "walktree skipping %s\n" % (pathname));def collectPath(path, isDir=False):if isDir:dirs.append(path);else:files.append(path);def checkNginxWritableDirs(paths):uid = pwd.getpwnam('nginx').pw_uid;gid = pwd.getpwnam('nginx').pw_gid;for d in paths:dstat = os.stat(d);if dstat.st_uid != uid:try:os.chown(d, uid, gid);except:logger(1, "chown(%s, nginx, nginx) failed\n" % (d));def checkOtherReadableDirs(paths, isDir=False):for d in paths:dstat = os.stat(d);if isDir:checkMode = 5;willBeMode = dstat.st_mode | stat.S_IROTH | stat.S_IXOTH;else:checkMode = 4;willBeMode = dstat.st_mode | stat.S_IROTH;if int(oct(dstat.st_mode)[-1:]) & checkMode != checkMode:try:os.chmod(d, willBeMode);except:logger(1, "chmod(%s, %d) failed\n" % (d, oct(willBeMode)));if __name__ == "__main__":for d in nginxWritableDirs:walktree(d, collectPath)dirs = dirs + files;checkNginxWritableDirs(dirs);dirs = [];files = [];for d in otherReadableDirs:walktree(d, collectPath)checkOtherReadableDirs(dirs, True);checkOtherReadableDirs(files, False);

OS. the chmod (path, mode) method should be very simple. It only requires two parameters: path and path mode, the following lists some common modes that can be used in this usage:

Stat. S_ISUID: Set user ID on execution. not commonly used

Stat. S_ISGID: Set group ID on execution. not commonly used

Stat. S_ENFMT: Record locking enforced. not commonly used

Stat. S_ISVTX: Save text image after execution. Save the text and image after execution.

Stat. S_IREAD: Read by owner. Read Permission for the owner

Stat. S_IWRITE: Write by owner. Write Permission to the owner

Stat. S_IEXEC: Execute by owner. permission on the owner

Stat. S_IRWXU: Read, write, and execute by owner. Read and write Permissions Of the owner

Stat. S_IRUSR: Read by owner. Read Permission for the owner

Stat. S_IWUSR: Write by owner. Write Permission for the owner

Stat. S_IXUSR: Execute by owner. permission on the owner

Stat. S_IRWXG: Read, write, and execute by group. Read and write permissions for people in the same group

Stat. S_IRGRP: Read by group. Read Permission for the same group

Stat. S_IWGRP: Write by group. Write Permission for the same group

Stat. S_IXGRP: Execute by group. Permission for execution in the same group

Stat. S_IRWXO: Read, write, and execute by others. Read and write permissions for other groups

Stat. S_IROTH: Read by others. Read Permission for other groups

Stat. S_IWOTH: Write by others. Write Permission for other groups

Stat. S_IXOTH: Execute by others. Permission to Execute other groups

>>> os.stat('test')posix.stat_result(st_mode=33204, st_ino=93328670, st_dev=18L, st_nlink=1, st_uid=30448, st_gid=1000, st_size=0, st_atime=1445932321, st_mtime=1445932321, st_ctime=1445932321)>>> os.stat('test').st_mode33204>>> oct(os.stat('test').st_mode)'0100664'>>> oct(os.stat('test').st_mode)[-3:]'664'

The above Python Method for viewing the file's read and write permissions is all the content shared by the editor. I hope you can give us a reference and support the help house.

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.