欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 查看文件的讀寫權(quán)限方法

 更新時間:2018年01月23日 15:42:17   作者:_Black鐵木真  
下面小編就為大家分享一篇Python 查看文件的讀寫權(quán)限方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

實例如下:

# -*- 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) 這個方法應(yīng)該很簡單,只需要2個參數(shù),一個是路徑,一個是說明路徑的模式,下面列出了這個用法中可以使用的一些常用的模式:

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. 在執(zhí)行之后保存文字和圖片

stat.S_IREAD: Read by owner. 對于擁有者讀的權(quán)限

stat.S_IWRITE: Write by owner. 對于擁有者寫的權(quán)限

stat.S_IEXEC: Execute by owner. 對于擁有者執(zhí)行的權(quán)限

stat.S_IRWXU: Read, write, and execute by owner. 對于擁有者讀寫執(zhí)行的權(quán)限

stat.S_IRUSR: Read by owner. 對于擁有者讀的權(quán)限

stat.S_IWUSR: Write by owner. 對于擁有者寫的權(quán)限

stat.S_IXUSR: Execute by owner. 對于擁有者執(zhí)行的權(quán)限

stat.S_IRWXG: Read, write, and execute by group. 對于同組的人讀寫執(zhí)行的權(quán)限

stat.S_IRGRP: Read by group. 對于同組讀的權(quán)限

stat.S_IWGRP: Write by group. 對于同組寫的權(quán)限

stat.S_IXGRP: Execute by group. 對于同組執(zhí)行的權(quán)限

stat.S_IRWXO: Read, write, and execute by others. 對于其他組讀寫執(zhí)行的權(quán)限

stat.S_IROTH: Read by others. 對于其他組讀的權(quán)限

stat.S_IWOTH: Write by others. 對于其他組寫的權(quán)限

stat.S_IXOTH: Execute by others. 對于其他組執(zhí)行的權(quán)限

>>> 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 查看文件的讀寫權(quán)限方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python圖像處理之圖片文字識別功能(OCR)

    Python圖像處理之圖片文字識別功能(OCR)

    Tesseract 是一個OCR 庫,目前由Google 贊助(Google 也是一家以O(shè)CR 和機器學習技術(shù)聞名于世的公司)。這篇文章主要介紹了Python圖像處理之圖片文字識別功能 OCR的相關(guān)知識,需要的朋友可以參考下
    2019-07-07
  • 使用Python寫一個量化股票提醒系統(tǒng)

    使用Python寫一個量化股票提醒系統(tǒng)

    這篇文章主要介紹了小白用Python寫了一個股票提醒系統(tǒng),迷你版量化系統(tǒng),完美的實現(xiàn)了實時提醒功能,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • PyCharm中New Directory 和 New Python Package的區(qū)別解析

    PyCharm中New Directory 和 New Python 

    python package這是一個特殊的目錄,因為在創(chuàng)建該python package的時候,系統(tǒng)會自動地生成一個py文件, init.py,這篇文章主要介紹了PyCharm中New Directory 和 New Python Package的區(qū)別,需要的朋友可以參考下
    2023-12-12
  • Python3.5字符串常用操作實例詳解

    Python3.5字符串常用操作實例詳解

    這篇文章主要介紹了Python3.5字符串常用操作,結(jié)合實例形式總結(jié)分析了Python3.5字符串輸入、輸出、格式化、切片以及各種常用操作函數(shù)相關(guān)使用技巧,需要的朋友可以參考下
    2019-05-05
  • Python如何輸出整數(shù)

    Python如何輸出整數(shù)

    在本篇文章里小編給大家分享的是關(guān)于Python輸出整數(shù)實例方法,有興趣的朋友們可以學習下。
    2020-06-06
  • Python編程實現(xiàn)及時獲取新郵件的方法示例

    Python編程實現(xiàn)及時獲取新郵件的方法示例

    這篇文章主要介紹了Python編程實現(xiàn)及時獲取新郵件的方法,涉及Python實時查詢郵箱及郵件獲取相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • python實現(xiàn)MySQL指定表增量同步數(shù)據(jù)到clickhouse的腳本

    python實現(xiàn)MySQL指定表增量同步數(shù)據(jù)到clickhouse的腳本

    這篇文章主要介紹了python實現(xiàn)MySQL指定表增量同步數(shù)據(jù)到clickhouse的腳本,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • pycharm創(chuàng)建scrapy項目教程及遇到的坑解析

    pycharm創(chuàng)建scrapy項目教程及遇到的坑解析

    這篇文章主要介紹了pycharm創(chuàng)建scrapy項目教程及遇到的坑解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08
  • python語言實現(xiàn)貪吃蛇游戲

    python語言實現(xiàn)貪吃蛇游戲

    這篇文章主要為大家詳細介紹了python語言實現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • pandas數(shù)據(jù)處理進階詳解

    pandas數(shù)據(jù)處理進階詳解

    這篇文章主要介紹了pandas數(shù)據(jù)處理進階詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10

最新評論