'sudo' changes enviroment variables ,such as : os.environ['HOME'], etc
加入脚本为evir.py
import os
if __name__ == "__main__":
print 'wsh'+os.environ['HOME']
print 'wsh'+ os.path.expandvars('$HOME')
print 'wsh'+ os.path.expanduser('~')
执行此脚本。有两种结果
执行此脚本。有两种结果
1 。 直接执行 treminal 输入:
python envir.py
结果:
wsh /home/wsh
wsh /home/wsh
wsh /home/wsh
2。 加上sudo指令
sudo python envir.py
结果:
wsh /root
wsh /root
wsh /root
so just use:
os.getlogin()
in conclusion:
if you want to get current user home in linux , while you have to get 'sudo' permission. that is to say . you need sudo to start up your python script.
just use "os.getlogin()", since other commands are changed because of sudo. then use can just to get current home directory use:
'/home/'+os.getlogin()
author: wsh
emiail: tongzhuodenilove@163.com