python 下chmod方法简单介绍
闲着无聊,想着如何能用python完全替代shell来管理服务器,呵呵,这个想法实在疯狂,首先来介绍下os模块下的chmod这个方法来替代shell下chmod这个命令呢,下面来看看这个方法的使用:
os.chmod(path,mode) 这个方法应该很简单,只需要2个参数,一个是路径,一个是说明路径的模式,下面列出了这个用法中可以使用的一些常用的模式:
-
stat.S_ISUID: Set user ID on execution. 不常用
-
stat.S_ISGID: Set group ID on execution. 不常用
-
stat.S_ENFMT: Record locking enforced. 不常用
-
stat.S_ISVTX: Save text p_w_picpath after execution. 在执行之后保存文字和图片
-
stat.S_IREAD: Read by owner.
-
对于拥有者读的权限
-
stat.S_IWRITE: Write by owner.
-
对于拥有者写的权限
-
stat.S_IEXEC: Execute by owner.
-
对于拥有者执行的权限
-
stat.S_IRWXU: Read, write, and execute by owner. 对于拥有者读写执行的权限
-
stat.S_IRUSR: Read by owner.
-
对于拥有者读的权限
-
stat.S_IWUSR: Write by owner.
-
对于拥有者写的权限
-
stat.S_IXUSR: Execute by owner.
-
对于拥有者执行的权限
-
stat.S_IRWXG: Read, write, and execute by group.
-
对于同组的人读写执行的权限
-
stat.S_IRGRP: Read by group.
-
对于同组读的权限
-
stat.S_IWGRP: Write by group.
-
对于同组写的权限
-
stat.S_IXGRP: Execute by group.
-
对于同组执行的权限
-
stat.S_IRWXO: Read, write, and execute by others.
-
对于其他组读写执行的权限
-
stat.S_IROTH: Read by others.
-
对于其他组读的权限
-
stat.S_IWOTH: Write by others.
-
对于其他组写的权限
-
stat.S_IXOTH: Execute by others.
-
对于其他组执行的权限
介绍以上这么多的模式,下面来看下实际的操作吧:
1: In [36]: import os
2:
3: In [37]: import stat
4:
5: In [38]: !touch abc
6:
7: In [39]: !ls -l abc
8: -rw-r--r-- 1 root root 0 6月 9 11:15 abc
9:
10: In [40]: os.chmod('abc',stat.S_IREAD)
11:
12: In [41]: !ls -l abc
13: -r-------- 1 root root 0 6月 9 11:15 abc
14:
15:
16: In [43]: os.chmod('abc',stat.S_IREAD+stat.S_IWOTH)
17:
18: In [44]: !ls -l abc
19: -r------w- 1 root root 0 6月 9 11:15 abc
20:
21: In [45]: os.chmod('abc',stat.S_IREAD+stat.S_IWOTH+stat.S_IXUSR)
22:
23: In [46]: !ls -l abc
24: -r-x----w- 1 root root 0 6月 9 11:15 abc
25:
26: In [47]: os.chmod('abc',stat.S_IREAD+stat.S_IWOTH+stat.S_IXUSR+stat.S_IRWX)
27: stat.S_IRWXG stat.S_IRWXO stat.S_IRWXU
28:
29: In [47]: os.chmod('abc',stat.S_IREAD+stat.S_IWOTH+stat.S_IXUSR+stat.S_IRWXO)
30:
31: In [48]: !ls -l abc
32: -r-x--x--x 1 root root 0 6月 9 11:15 abc
33:
34: In [49]: os.chmod('abc',stat.S_IRWX)
35: stat.S_IRWXG stat.S_IRWXO stat.S_IRWXU
36:
37: In [49]: os.chmod('abc',stat.S_IRWXO+stat.S_IRW)
38: stat.S_IRWXG stat.S_IRWXO stat.S_IRWXU
39:
40: In [49]: os.chmod('abc',stat.S_IRWXO+stat.S_IRWXG+stat.S_IRWX)
41: stat.S_IRWXG stat.S_IRWXO stat.S_IRWXU
42:
43: In [49]: os.chmod('abc',stat.S_IRWXO+stat.S_IRWXG+stat.S_IRWXO)
44:
45: In [50]: !ls -l abc
46: ---x---rw- 1 root root 0 6月 9 11:15 abc
47:
48: In [51]:
相关文章