用笨办法实现(在python 2.4下可以用):

返回列表:

#! /usr/bin/env python
#-*- coding:utf-8 -*-

''' 概述:从/etc/passwd获取系统用户名、用户ID、shell,以列表形式返回
输出:['root, 0, /bin/bash', 'bin, 1, /sbin/nologin', ...]

Create data: 2012-02-09
Version: 1.0
Author: 沈涛

'''

import platform

def getUserName(UserType):
    user_list = []
    
    OSType = platform.system()
    
    if (OSType == "Linux"):
        fp = open('/etc/passwd').readlines()
        for line in fp:
            user_list = "%s, %s, %s" % (line.split('\n')[0].split(':')[0],
                                   line.split('\n')[0].split(':')[2],
                                   line.split('\n')[0].split(':')[6])
        
            
    elif (OSType == "Windows"):
        print "Windows System"
        
    return user_list


返回字典:

#! /usr/bin/env python
#-*- coding:utf-8 -*-

''' 概述:从/etc/passwd获取系统用户名、用户ID、shell,以字典形式返回
输出:{'sync': ['5', '/bin/sync'], 'gg': ['506', '/bin/bash'], ...}

Create data: 2012-02-09
Version: 1.0
Author: 沈涛

'''

import platform

def getUserName():
    #user_list = {}
    item = {}

    OSType = platform.system()
    print OSType

    if (OSType == "Linux"):
        fp = open('/etc/passwd').readlines()
        for line in fp:
            item1 = line.split('\n')[0].split(':')[0]
            item2 = line.split('\n')[0].split(':')[2]
            item3 = line.split('\n')[0].split(':')[6]
            item[item1] = [item2, item3]

    elif (OSType == "Windows"):
        print "Windows System"

    print item

if __name__ == "__main__":
    getUserName()




Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐