如何用Python将共享网络文件设为只读?

0 投票
1 回答
714 浏览
提问于 2025-04-15 15:02

使用Python时,如何正确地将一个文件设置为只读,前提是这个文件位于网络共享上(由Windows 2003服务器提供)?

我在OS X(10.6.1)上运行Python 2.6.2。

当路径是本地文件时,下面的代码会抛出一个异常(这是预期的),但当路径指向Windows共享时,os.chmod似乎没有任何效果。

import os, stat

path = '/Volumes/Temp/test.txt'

# Create a test file.
open(path, 'w').close()

# Make the file read-only.
os.chmod(path, stat.S_IREAD)

# Try writing to it again. This should fail.
open(path, 'w').close()

1 个回答

0

我很确定,你需要在本地的SAMBA服务器上(也就是在/etc/samba/smb.conf这个文件里)设置好相关的配置,才能让它按照你想要的方式工作。如果这个配置文件没有设置正确,有很多方法可以绕过权限检查。

撰写回答