python文件目录无权限_使用python检查文件夹/文件ntfs权限

As the question title might suggest, I would very much like to know of the way to check the ntfs permissions of the given file or folder (hint: those are the ones you see in the "security" tab). Basically, what I need is to take a path to a file or directory (on a local machine, or, preferrably, on a share on a remote machine) and get the list of users/groups and the corresponding permissions for this file/folder. Ultimately, the application is going to traverse a directory tree, reading permissions for each object and processing them accordingly.

Now, I can think of a number of ways to do that:

parse cacls.exe output -- easily done, BUT, unless im missing something, cacls.exe only gives the permissions in the form of R|W|C|F (read/write/change/full), which is insufficient (I need to get the permissions like "List folder contents", extended permissions too)

xcacls.exe or xcacls.vbs output -- yes, they give me all the permissions I need, but they work dreadfully slow, it takes xcacls.vbs about ONE SECOND to get permissions on a local system file. Such speed is unacceptable

win32security (it wraps around winapi, right?) -- I am sure it can be handled like this, but I'd rather not reinvent the wheel

Is there anything else I am missing here?

解决方案

Unless you fancy rolling your own, win32security is the way to go. There's the beginnings of an example here:

If you want to live slightly dangerously (!) my in-progress winsys package is designed to do exactly what you're after. You can get an MSI of the dev version here:

or you can just checkout the svn trunk:

To do what you describe (guessing slightly at the exact requirements) you could do this:

import codecs

from winsys import fs

base = "c:/temp"

with codecs.open ("permissions.log", "wb", encoding="utf8") as log:

for f in fs.flat (base):

log.write ("\n" + f.filepath.relative_to (base) + "\n")

for ace in f.security ().dacl:

access_flags = fs.FILE_ACCESS.names_from_value (ace.access)

log.write (u" %s => %s\n" % (ace.trustee, ", ".join (access_flags)))

TJG

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您想加密一个文件夹或zip文件,可以使用Python中的加密库来实现。以下是一些常用的加密库: 1. pyAesCrypt:这是一个用于加密和解密文件Python库。它使用AES加密算法,并且可以处理大文件。 2. cryptography:这是一个用于加密和解密数据的Python库。它支持多种加密算法,包括AES、DES、RSA等。 3. PyCrypto:这是一个Python密码库,支持多种加密算法,包括AES、DES、RSA、Blowfish等。 下面是使用pyAesCrypt加密文件夹或zip文件的代码示例: ```python import pyAesCrypt import os # 定义加密函数 def encrypt_folder(password, src_folder, dest_zip): bufferSize = 64 * 1024 pyAesCrypt.encryptFile(src_folder, dest_zip, password, bufferSize) # 定义解密函数 def decrypt_folder(password, src_zip, dest_folder): bufferSize = 64 * 1024 pyAesCrypt.decryptFile(src_zip, dest_folder, password, bufferSize) # 加密文件夹 password = "mypassword" src_folder = "myfolder" dest_zip = "myfolder.zip" encrypt_folder(password, src_folder, dest_zip) # 解密文件夹 src_zip = "myfolder.zip" dest_folder = "myfolder_decrypted" decrypt_folder(password, src_zip, dest_folder) ``` 在上面的代码中,我们使用pyAesCrypt库来加密和解密文件夹。首先,我们定义了两个函数:encrypt_folder和decrypt_folder,分别用于加密和解密文件夹。接着,我们定义了一个密码变量和两个文件路径变量,用于指定要加密的文件夹和加密后的zip文件。最后,我们调用encrypt_folder函数来加密文件夹,调用decrypt_folder函数来解密文件夹

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值