unpickled 字节数组的文件写入默认没有设置权限 [英] File write of unpickled byte array has no permissions set by default

查看:31
本文介绍了unpickled 字节数组的文件写入默认没有设置权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经反序列化了这本字典中的文件数据,排列如下 -

I have deserialized file data in this dictionary arranged as follows -

[filename1 : bytearray with file contents]  
[filename2 : bytearray with file contents]  
[filename3 : bytearray with file contents]  
...

现在,当我使用

    for f,bArr in depickled_.items():
        with open(os.path.join(r"S:\test", f), "wb") as fWr:
            fWr.write(bytearray(bArr))
            fWr.close() # <- probably redundant

文件正在按预期写入,但默认情况下它们没有应用权限,我觉得这很奇怪.因此,我无法按原样打开任何写入的文件,但是当我摆弄安全设置以允许自己读取访问权限时,它们会按预期打开.

The files are getting written as expected, but they have no permissions applied to them by default which I find odd. Therefore, I cannot open any of the written files as is, but when I fiddle with the security settings to allow myself read access then they open as expected.

知道出了什么问题以及如何解决吗?我是这台计算机的唯一管理员(和用户).

Any idea what's going wrong and how I can fix it? I am the sole administrator (and user) of this computer.

更多信息:

  • Python 3.7 版
  • Windows 10 家庭版

推荐答案

使用 pywin32 模块添加了一些权限.

Added some permissions using pywin32 module.

import win32security
import ntsecuritycon as con
from win32 import win32api

for f,bArr in depickle_.items():
        full_path = os.path.join(r"S:\test", f) 
        # with open(full_path, "wb+") as fWr:
        fd = os.open(full_path, os.O_CREAT | os.O_WRONLY, 0o777) # will probably work for linux
        with open(fd, 'wb') as fWr:
            fWr.write(bytearray(bArr))
            fWr.close()

    # user, domain, type = win32security.LookupAccountName ("", win32api.GetUserName())
    user, domain, type = win32security.LookupAccountName ("", "Everyone")
    sd = win32security.GetFileSecurity(full_path, win32security.DACL_SECURITY_INFORMATION)
    dacl = sd.GetSecurityDescriptorDacl()

    # Delete all existing permissions        
    for index in range(0, dacl.GetAceCount()):
        dacl.DeleteAce(0)

    dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.FILE_ALL_ACCESS, user)
    sd.SetSecurityDescriptorDacl(1, dacl, 0) 
    win32security.SetFileSecurity(full_path, win32security.DACL_SECURITY_INFORMATION, sd)

这篇关于unpickled 字节数组的文件写入默认没有设置权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆