91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python 查看文件的讀寫權限方法

發布時間:2020-08-21 06:54:38 來源:腳本之家 閱讀:259 作者:_Black鐵木真 欄目:開發技術

實例如下:

# -*- 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 查看文件的讀寫權限方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

盐源县| 鹤山市| 新田县| 昌江| 赤峰市| 华阴市| 来凤县| 新邵县| 高邮市| 油尖旺区| 顺昌县| 霍林郭勒市| 丰镇市| 清丰县| 六盘水市| 漾濞| 突泉县| 林甸县| 衡阳市| 建始县| 景洪市| 贵德县| 清涧县| 安龙县| 南部县| 江北区| 芜湖县| 赣榆县| 静海县| 哈巴河县| 盐津县| 周宁县| 象山县| 达日县| 广东省| 疏勒县| 临邑县| 布尔津县| 鸡东县| 洛阳市| 江川县|