python修改文件夹权限

在python中修改文件权限比较复杂,以下为修改成700的示例:
>>> import os, sys, stat
>>> os.chmod(“test”,stat.S_IRWXU)
其中stat有多种属性,在http://docs.python.org/2/library/stat.html#stat.S_ISUID中有详细介绍,部分如下:
stat.S_ISUID
Set UID bit.
stat.S_ISGID
Set-group-ID bit. This bit has several special uses. For a directory it indicates that BSD semantics is to be used for that directory: files created there inherit their group ID from the directory, not from the effective group ID of the creating process, and directories created there will also get the S_ISGID bit set. For a file that does not have the group execution bit (S_IXGRP) set, the set-group-ID bit indicates mandatory file/record locking (see also S_ENFMT).
stat.S_ISVTX
Sticky bit. When this bit is set on a directory it means that a file in that directory can be renamed or deleted only by the owner of the file, by the owner of the directory, or by a privileged process.
stat.S_IRWXU
Mask for file owner permissions.
stat.S_IRUSR
Owner has read permission.
stat.S_IWUSR
Owner has write permission.
stat.S_IXUSR
Owner has execute permission.
stat.S_IRWXG
Mask for group permissions.
stat.S_IRGRP
Group has read permission.
stat.S_IWGRP
Group has write permission.
stat.S_IXGRP
Group has execute permission.
stat.S_IRWXO
Mask for permissions for others (not in group).
stat.S_IROTH
Others have read permission.
stat.S_IWOTH
Others have write permission.
stat.S_IXOTH
Others have execute permission.
在chmod的操作中,即可以按上面的值进行设置,也可按位或的方式进行组合。以下为设置755:
>>> import os, sys, stat
>>> os.chmod(“test”,stat.S_IRWXU|stat.S_IRGRP|stat.S_IXGRP|stat.S_IROTH|stat.S_IXOTH)
对python不熟悉的话,还是觉得shell方便:
>>> import time
>>> import os
>>> import sys
>>> from subprocess import Popen, PIPE
>>> os.system(‘chmod 700 test’)
下面是最简洁的方式:
>>> import time
>>> import os
>>> import sys
>>> os.chmod(path,700)
此条目发表在IT, Linux, 未分类分类目录。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。

验证图片

*