【发布时间】: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