python 查看文件夹权限组和用OS模块操作文件夹
@建议操作server服务器文件夹时可以映射网络驱动盘
import win32security
import ntsecuritycon as con
FILENAME = r'D:\tmp\acc_test' #文件夹路径
sd = win32security.GetFileSecurity(FILENAME, win32security.DACL_SECURITY_INFORMATION)
dacl = sd.GetSecurityDescriptorDacl()
ace_count = dacl.GetAceCount() #显示具有文件夹权限用户(组)数量
print('Ace count:', ace_count)
for i in range(0, ace_count):
rev, access, usersid = dacl.GetAce(i)
user, group, type = win32security.LookupAccountSid('', usersid)
print('User: {}/{}'.format(group, user), rev, access)
User: user (0, 0) 1179817 #只有该文件夹,查看权限
User: admin (0, 0) 1245631 #只有该文件夹,修改权限
User: administrator (0, 3) 2032127 #此文件夹,子文件夹 全部权限
#删除文件夹权限
Now you are able to read old ACEs and deleting old ones is quite simple:
for i in range(0, ace_count):
dacl.DeleteAce(0)
#掉用add增加权限
And after that you can just add privileges by calling AddAccessAllowedAceEx() [MSDN]:
userx, domain, type = win32security.LookupAccountName ("", "your.user")
usery, domain, type = win32security.LookupAccountName ("", "other.user")
dacl.AddAccessAllowedAceEx(win32security.ACL_REVISION, 3, 2032127, userx) # 完全控制 @中间3代表权限会被后面文件夹继承 0:不会
dacl.AddAccessAllowedAceEx(win32security.ACL_REVISION, 3, 1179785, usery) # 只读权限
sd.SetSecurityDescriptorDacl(1, dacl, 0) #可能没有必要
win32security.SetFileSecurity(FILENAME, win32security.DACL_SECURITY_INFORMATION, sd)
I've taken numbers 3, 2032127 and 1179785 from the listing in first half of the script (before running the script I've set up privileges in Explorer->Right Click->Properties->Security->Advanced):
#
#
#例子
# try:
# permit = '1179817'
# acegroup = 'Users'
# folder = 'Z:\\50.测试文件夹\\01.2'
# sd = win32security.GetFileSecurity(folder, win32security.DACL_SECURITY_INFORMATION)
# dacl = sd.GetSecurityDescriptorDacl()
# user1, domain, type = win32security.LookupAccountName('', acegroup)
#
# permisson = int(permit)
# dacl.AddAccessAllowedAceEx(win32security.ACL_REVISION, 0, permisson, user1) #中间的0 表示只有该文件夹 3表示此文件夹,子文件
# sd.SetSecurityDescriptorDacl(1, dacl, 0) # may not be necessary
# win32security.SetFileSecurity(folder, win32security.DACL_SECURITY_INFORMATION, sd)
# except Exception as e:
# print(e)
#python 的os模块操控文件夹
#import os
#import shutil
# try:
# os.mkdir(r'Z:\50.测试文件夹\01.2') #新建文件夹
# except Exception as e:
# print(e)
# try:
# os.rename(r'Z:\50.测试文件夹\01.2',r'Z:\50.测试文件夹\01.3') #重命名文件夹
# except Exception as e:
# print(e)
# try:
# shutil.rmtree(r'Z:/50.测试文件夹/01.3') ##删除名文件夹
# except Exception as e:
# print(e)
PS.调用cmd操控文件夹
http://jingyan.baidu.com/article/d3b74d64c853081f77e60929.html 下载安装pywin32
#创建文件夹
def createfolder(request):
folder=r'Z:/50.测试文件夹/01.3'
result={}
try:
#用wmi连接到远程服务器
pythoncom.CoInitialize()
conn = wmi.WMI(computer=‘192.168.0.1’, user='user', password='user')
cmd_callbat=r"cmd.exe /c mkdir %s" % folder
conn.Win32_Process.Create(CommandLine=cmd_callbat) #执行bat文件
result['isSuccess']=True
result['message']=' 添加成功'
except Exception as e:
print(e)
result['isSuccess'] = False
result['message'] = ' 添加失败'+e
response = HttpResponse()
response['Content-Type'] = "text/javascript"
response.write(json.dumps(result))
return response
来源http://jingyan.baidu.com/article/f3ad7d0ffe8b1409c2345b43.html
http://www.programcreek.com/python/index/478/win32security
python 查看文件夹权限组和用OS模块操作文件夹的更多相关文章
- python中OS模块操作文件和目录
在python中执行和操作目录和文件的操作是通过内置的python OS模块封装的函数实现的. 首先导入模块,并查看操作系统的类型: >>> import os os.name # ...
- Python OS模块操作文件和目录
#-*-coding:utf-8-*- import os import shutil ###############OS模块############## #获得当前python脚本的工作目录 os. ...
- os模块操作文件
os模块: path=os.path.join(os.path.dirname(os.path.dirname(__file__)),'images') path:运行脚本的当前文件下的上一个文件的地 ...
- python——os模块操作文件
- ubuntu chmod 无法更改 文件夹权限 系统提示“不允许的操作 2、linux 如何修改只读文件 3、ubuntu安装
1.ubuntu chmod 无法更改 文件夹权限 系统提示“不允许的操作 答案:需要超级用户权限 sudo 2.linux 如何修改只读文件 答案:可以使用chmod命令,为改文件提供其他的权限.u ...
- 【python基础语法】OS模块处理文件绝对路径,内置的异常类型、捕获、处理(第9天课堂笔记)
import os """ 通过文件的路径去打开文件 相对路径:相对当前的工作路径去定位文件位置 .:代表当前路径 ..:代表上一级路径(父级路径) 绝对路径:相对于电脑 ...
- os模块、文件压缩 、匹配文件后缀名:fnmatch glob
一.os模块 os模块:是python是系统交互的模块 import os # 0平台信息的一些操作 python是夸平台的,所以内部兼容了不同的平台 1. os.name # 操作系统 nt是win ...
- python基础之os模块操作
# os模块 目录相关内置库import os# . 当前目录 .. 返回上一级目录# 1. os.path.abspath() --获取当前文件的绝对路径(不包含os模块.py) pwd# path ...
- (原创)Python文件与文件系统系列(2)——os模块对文件、文件系统操作的支持
os模块的功能主要包括文件系统部分和进程管理部分,这里介绍其中与文件系统相关的部分. 当请求操作系统执行操作失败时,os模块抛出内置异常 exceptions.OSError 的实例,可以通过 os. ...
随机推荐
- HighCharts基本用法
var options={ chart: {type: 'column',renderTo: 'ChartDesigner1'},//type :图表类型(柱状图,饼状图),renderTo :指向页 ...
- OD: Kernel Vulnerabilities
内核漏洞概述 内核漏洞的分类 运行在 Ring0 上的操作系统内核.设备驱动.第三方驱动能共享同一个虚拟地址空间,可以完全访问系统空间的所有内存,而不像用户态进程那样拥有独立私有的内存空间.由于内核程 ...
- 地址栏访问Action,后来方法执行两次
SSH框架,在地址栏输入URL访问Action,后台访问会访问两次.很奇怪. 经排查,最终问题在于方法名称写错了.将getOpinionByPN()修改成queryOpinionByPN(),没有问题 ...
- Java实现文件上传
最近自己在做一个小系统玩的时候涉及到了文件的上传,于是在网上找到Java上传文件的方案,最后确定使用common-fileupload实现上传操作. 需求说明 用户添加页面有一个“上传”按钮,点击按钮 ...
- Android虚拟机GenyMotion-- 遇到的问题
问题: android studio 检测不到 genymotion 原因:没有设置genymotion的adb,也就是sdk的路径. 解决方法:打开genymotion的主页面,设置sdk的位置为你 ...
- iOS 不同类之间的传值
iOS是面向对象开发的,有很多不同的类,很多时候会遇到类与类之间的"交流"需求,比如通知.传递数值等等,(通知可以用nsnotificationcenter来做, 以后总结)下面主 ...
- 【HAOI2007】理想的正方形
[问题描述] 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. [输入] 第一行为3个整数,分别表示a,b,n的值第二行至第a+1行每行 ...
- Java学习----你的选择是什么-条件结构
import java.util.Scanner; public class Student { public static void main(String[] args) { byte money ...
- ubantu-命令-进入超级用户
sudo su; 建立文件夹 mkdir ulike_work;
- js学习笔记之:时间(二)
今天来了解一下js中定时器的两种用法.js中包括2种定时器,分别是: 间隔型定时器:setInterval(开) clearInterval (关) 延 ...