Python 查看文件的读写权限方法


Posted in Python onJanuary 23, 2018

实例如下:

# -*- coding: utf-8 -*-
# @author flynetcn
import 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.chmod(path,mode) 这个方法应该很简单,只需要2个参数,一个是路径,一个是说明路径的模式,下面列出了这个用法中可以使用的一些常用的模式:

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. 对于其他组执行的权限

>>> 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_mode
33204
>>> oct(os.stat('test').st_mode)
'0100664'
>>> oct(os.stat('test').st_mode)[-3:]
'664'

以上这篇Python 查看文件的读写权限方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python中实现定制类的特殊方法总结
Sep 28 Python
Python中join和split用法实例
Apr 14 Python
Python subprocess模块功能与常见用法实例详解
Jun 28 Python
Win8.1下安装Python3.6提示0x80240017错误的解决方法
Jul 31 Python
python for和else语句趣谈
Jul 02 Python
springboot配置文件抽离 git管理统 配置中心详解
Sep 02 Python
python能做什么 python的含义
Oct 12 Python
Python中itertools的用法详解
Feb 07 Python
Numpy实现卷积神经网络(CNN)的示例
Oct 09 Python
Pycharm安装Qt Design快捷工具的详细教程
Nov 18 Python
Pytorch可视化的几种实现方法
Jun 10 Python
如何正确理解python装饰器
Jun 15 Python
Python3 中文文件读写方法
Jan 23 #Python
Python3之文件读写操作的实例讲解
Jan 23 #Python
Python实现邮件的批量发送的示例代码
Jan 23 #Python
python实现自动发送邮件发送多人、群发、多附件的示例
Jan 23 #Python
python正则表达式面试题解答
Apr 28 #Python
Django中使用celery完成异步任务的示例代码
Jan 23 #Python
python3 发送任意文件邮件的实例
Jan 23 #Python
You might like
php将文件夹打包成zip文件的简单实现方法
2016/10/04 PHP
PHP正则删除HTML代码中宽高样式的方法
2017/06/12 PHP
PHP中ltrim()函数的用法与实例讲解
2019/03/28 PHP
用js判断浏览器是否是IE的比较好的办法
2007/05/08 Javascript
javascript innerHTML、outerHTML、innerText、outerText的区别
2008/11/24 Javascript
javascript调试说明
2010/06/07 Javascript
jquery的冒泡事件的阻止与允许(三种实现方法)
2013/02/01 Javascript
js 获取input点选按钮的值的方法
2014/04/14 Javascript
JavaScript学习笔记之JS函数
2015/01/22 Javascript
jquery实现两边飘浮可关闭的对联广告
2015/11/27 Javascript
vue.js实现表格合并示例代码
2016/11/30 Javascript
js原生之焦点图转换加定时器实例
2016/12/12 Javascript
详解ES6中的代理模式——Proxy
2018/01/08 Javascript
Bootstrap开发中Tab标签页切换图表显示问题的解决方法
2018/07/13 Javascript
微信小程序实现横向增长表格的方法
2018/07/24 Javascript
nodejs制作小爬虫功能示例
2020/02/24 NodeJs
[01:02:18]VGJ.S vs infamous Supermajor 败者组 BO3 第一场 6.4
2018/06/05 DOTA
Python中if __name__ == "__main__"详细解释
2014/10/21 Python
Python中返回字典键的值的values()方法使用
2015/05/22 Python
Python socket编程实例详解
2015/05/27 Python
python print出共轭复数的方法详解
2019/06/25 Python
Python3 字典dictionary入门基础附实例
2020/02/10 Python
Pycharm github配置实现过程图解
2020/10/13 Python
django使用多个数据库的方法实例
2021/03/04 Python
阿迪达斯墨西哥官方网站:adidas墨西哥
2017/11/03 全球购物
台湾网友喜爱的综合型网路购物商城:Yahoo! 奇摩购物中心
2018/03/10 全球购物
全球在线商店:BerryLook
2019/04/14 全球购物
迪卡侬中国官网:Decathlon中国
2020/08/10 全球购物
Windows和Linux动态库应用异同
2016/04/17 面试题
住房公积金接收函
2014/01/09 职场文书
建议书的格式
2014/05/12 职场文书
常务副县长“四风”个人对照检查材料思想汇报
2014/10/02 职场文书
python OpenCV学习笔记
2021/03/31 Python
解决mysql:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO/YES)
2021/06/26 MySQL
Nginx的基本概念和原理
2022/03/21 Servers
MySQL慢查询中的commit慢和binlog中慢事务的区别
2022/06/16 MySQL