【问题标题】:Setting group permissions with python使用 python 设置组权限
【发布时间】:2023-03-28 16:42:01
【问题描述】:

这是我的设置:

我有一个 VirtualMachine (Ubuntu 14.04. LTS),其中运行 PostgreSQL/PostGIS 数据库。

在 QGIS 中使用 Windows 7,我连接到该数据库并将要素图层加载到我的 GIS 项目中。

使用一些 python 代码,我创建了一个带有磁贴 ID 和一些信息的文件。

import os
import io
import time

layer=None
for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
if lyr.name() == "fishnet_final":
    layer = lyr

for f in layer.selectedFeatures():
    pth = os.path.join(os.path.dirname(r'H:\path_to_file\'), str(f['name']) + "_" + str(time.strftime("%Y-%m-%d")) + "_" + str(f['country']) + ".txt")
    fle = open(pth,'wb')    
    fle.writelines(str(f['name']))
    fle.write('\n')
    fle.write(str(time.strftime("%Y-%d-%m")))
    fle.write('\n')
    fle.write(str(f['country']))
    fle.write('\n')
    fle.close()
    os.rename(pth, pth.replace(' ', ''))

文件有权限:

-rwx------

我还想为我的群组和其他人设置相同的权限。

-rwxrwxrwx

我试过了:

import shlex
command=shlex.split("chmod 777 r'H:\path_to_file\file.txt'") 
subprocess.call(command)

没有成功。

起作用的是:

command=shlex.split("touch r'H:\path_to_file\file.txt'")

command=shlex.split("rm r'H:\path_to_file\file.txt'")

为什么 chmod 命令不起作用?

在 UNIX 下,我可以对这个文件进行 chmod,而且我是 Windows 中的同一个用户。

我也尝试了 os.chmod 方法。但没有成功。

import os, stat
st = os.stat(r'H:\path_to_file\file.txt')
os.chmod(r'H:\path_to_file\file.txt', st.st_mode | 0o111 )

更新

当我在 UNIX (Solaris) 下执行“chmod 777 文件”时,权限是

-rwxrwxrwx

我现在能做的就是在GIS项目中在Windows下降级/移除权限:

subprocess.call(r'chmod 400 "H:\path_to_file\file.txt"', shell=True)
0
-r-xr-xr-x

使用此命令,我会在 python 控制台输出中获得 0 反馈

当我对新文件执行 chmod 777 时,我也收到了 0 反馈,但没有任何反应。

问题是我只能降级权限。我无法设置新权限!

【问题讨论】:

  • 你能打印每个subprocess.call (...)的返回吗?这将有助于定位错误。
  • 您是在 Windows 还是 Linux 机器上运行脚本?如果 H: 驱动器是 Samba 共享文件夹,则在 Windows 机器上运行我的 Windows 答案可能会起作用。如果没有,您可能需要在 Linux 服务器的/etc/samba/smb.conf 文件中设置权限。
  • 我在 Windows 中运行脚本。

标签: python file-permissions chmod shlex


【解决方案1】:

你的 shell 命令中的 r 字符是什么意思?你的意思是把它放在整个字符串的前面吗? 你检查过哪个文件是touch生成的吗?

当我尝试您的示例时,它会运行以下命令:['touch', 'rH:\\path_to_file\x0cile.txt'],即创建文件 rH:\path_to_file\file.txt

这对我来说很好用:

command=shlex.split("chmod 777 'H:\path_to_file\file.txt'") subprocess.call(command)

【讨论】:

  • 根据您的建议,我又试了一次,但还是不行。我已经更新了我的答案。
【解决方案2】:

试试这个(我现在没有Linux机器来测试它):

import subprocess
subprocess.call(r'chmod 777 "H:\path_to_file\file.txt"', shell=True)

如果文件名是用户提供的,出于安全原因,您应该避免使用shell=True。你可以试试:

filename = r"H:\path_to_file\file.txt"
subprocess.call(['chmod','777',filename])

【讨论】:

    【解决方案3】:

    来自os module documentation

    注意:虽然 Windows 支持 chmod(),但您只能使用它设置文件的只读标志(通过 stat.S_IWRITE 和 stat.S_IREAD 常量或相应的整数值)。 所有其他位都被忽略。

    对于 Windows 权限,您管理 ACL。改编自another answer,您需要pywin32 库:

    import win32security
    import ntsecuritycon as con
    
    FILENAME = r"H:\path_to_file\file.txt"
    
    user, domain, type = win32security.LookupAccountName ("", "Your Username")
    
    sd = win32security.GetFileSecurity(FILENAME, win32security.DACL_SECURITY_INFORMATION)
    dacl = sd.GetSecurityDescriptorDacl()   # instead of dacl = win32security.ACL()
    
    dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.FILE_ALL_ACCESS, user)
    
    sd.SetSecurityDescriptorDacl(1, dacl, 0)   # may not be necessary
    win32security.SetFileSecurity(FILENAME, win32security.DACL_SECURITY_INFORMATION, sd)
    

    con.FILE_ALL_ACCESS 标志更改为您需要的标志。

    【讨论】:

    • 感谢您的回答。这个答案中的步骤我仍然没有成功。明天再试一次。
    • 您是否将“您的用户名”字符串更改为您的实际用户名?
    猜你喜欢
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2011-02-14
    • 2013-11-04
    相关资源
    最近更新 更多