你的分享就是我们的动力 ---﹥

Linux获取用户主目录

时间:2016-11-25 17:53来源:www.chengxuyuans.com 点击:
#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
import os


class get_home_path(object):
def __init__(self, username):
self.username = username
self.user_dict = {}

def read_passwd(self):
with open("/etc/passwd", "r") as user_info:
for f in user_info.readlines():
f_list = f.strip('\n').split(':')
self.user_dict[f_list[0]] = f_list[5]
return self.user_dict

def get_home_dir(self):
user_dict = self.read_passwd()
try:
home_dir = user_dict[self.username]
return home_dir
except Exception, e:
print "%s user not exist!" % e
sys.exit()

if __name__ == '__main__':
if len(sys.argv) == 1:
print os.path.expanduser('~')
elif len(sys.argv) == 2:
username = sys.argv[1]
get_path = get_home_path(username)
print get_path.get_home_dir()
else:
print "Parameter input is incorrect,more than one."

转载注明地址:http://www.chengxuyuans.com/Python/105133.html

推荐文章