python 判断当前执行用户是否是 root 用户


非 root 用户下

os-liming@os-W330-H30:~$ python
Python 3.6.2 (default, Jul 17 2017, 13:39:29) 
[GCC 6.4.0 20170704] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print(os.getuid())
1000
>>> assert os.getuid() == 0, 'must use root'
Traceback (most recent call last):
  File "", line 1, in 
AssertionError: must use root

root 下

root@os-W330-H30:/# python
Python 3.6.2 (default, Jul 17 2017, 13:39:29) 
[GCC 6.4.0 20170704] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> assert os.getuid() == 0 , 'must use root'
>>> print(os.getuid())
0