本节主要内容:
python直接用linux管道输出,输出权限到info文件可以写成python filePermissions.py > info。然后,给info加执行权限即可。
例1,python获得文件或文件夹权限
#! /usr/bin/env python
#-*- coding: utf-8 -*-
#site: www.jb200.com
#
import os,stat
def WalkDir(dir, file_callback=None):
for root, dirs, files in os.walk(dir):
for d in dirs:
#print d
if(not d[0] == "."):
file_path = os.path.join(root, d)
if file_callback: file_callback(file_path)
for root, dirs, files in os.walk(dir):
for f in files:
#print f
if(not f[0] == "."):
file_path = os.path.join(root, f)
if file_callback: file_callback(file_path)
def LogFile(file):
try:
fileStats = os.stat(file)
#print file
#print fileStats.st_mode
#print oct(stat.S_IMODE(fileStats.st_mode))
#print fileStats.st_uid
#print fileStats.st_gid
fileInfo = 'chmod ' + oct(stat.S_IMODE(fileStats.st_mode)) + ' ' + file
print fileInfo
except:
pass
if __name__ == "__main__":
path = raw_input('')
WalkDir(path, LogFile)
例2,获得用户和组信息
#! /usr/bin/env python
#-*- coding: utf-8 -*-
#site: www.jb200.com
import os,stat
def WalkDir(dir, file_callback=None):
for root, dirs, files in os.walk(dir):
for d in dirs:
#print d
if(not d[0] == "."):
file_path = os.path.join(root, d)
if file_callback: file_callback(file_path)
for root, dirs, files in os.walk(dir):
for f in files:
#print f
if(not f[0] == "."):
file_path = os.path.join(root, f)
if file_callback: file_callback(file_path)
def LogFile(file):
try:
fileStats = os.stat(file)
#print file
#print fileStats.st_mode
#print oct(stat.S_IMODE(fileStats.st_mode))
#print fileStats.st_uid
#print fileStats.st_gid
fileInfo = 'chown ' + str(fileStats.st_uid) +':' + str(fileStats.st_gid) + ' ' + file
print fileInfo
except:
pass
if __name__ == "__main__":
path = raw_input('')
WalkDir(path, LogFile)