Linux Get user home directory

Source: Internet
Author: User

#!/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."

Linux Get user home directory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.