这是我的设置:
我有一个 VirtualMachine (Ubuntu 14.04.LTS),其中运行着一个 PostgreSQL/PostGIS 数据库。
在 QGIS 中使用 Windows 7,我连接到这个数据库并将要素层加载到我的 GIS 项目中。
我使用一些 python 代码创建了一个包含图 block 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
我现在能做的就是在Windows下对GIS项目降级/取消权限:
subprocess.call(r'chmod 400 "H:\path_to_file\file.txt"', shell=True)
0
-r-xr-xr-x
通过这个命令,我在 python 控制台输出中得到了一个 0
反馈
当我对新文件执行 chmod 777 但没有任何反应时,我也得到了 0
反馈。
问题是我只能降级权限。我无法设置新权限!
最佳答案
Note: Although Windows supports chmod(), you can only set the file’s read-only flag with it (via the stat.S_IWRITE and stat.S_IREAD constants or a corresponding integer value). All other bits are ignored.
对于 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
标志更改为您需要的标志。
关于python - 使用python设置组权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34338248/