Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### 1 (6 lines)
- def passwd_data(filename):
- fields='username:password:uid:gid:fullname:homedir:shell'.split(':')
- FILE = open(filename, "r")
- passwdfile=[y.strip() for y in FILE.readlines()]
- FILE.close()
- return [dict(zip(fields, line.split(':'))) for line in passwdfile]
- ### 2 (9 lines, but stripallf is a generic, reusable function)
- def stripallf(filename):
- FILE = open(filename, "r")
- lines=[y.strip() for y in FILE.readlines()]
- FILE.close()
- return lines
- def passwd_data2(filename):
- return [dict(zip('username:password:uid:gid:fullname:homedir:shell'.split(':'),
- line.split(':'))) for line in stripallf(filename)]
- ### 3 (10 lines, 1 import)
- from copy import deepcopy
- def passwd_data3(filename):
- dic={}; lst=[]
- FILE = open(filename, "r")
- for line in FILE:
- (dic['username'], dic['password'], dic['uid'], dic['gid'], dic['fullname'], dic['homedir'], dic['shell']) = line.strip().split(':')
- lst.append(deepcopy(dic))
- FILE.close()
- return lst
- ### 4 (11 lines)
- class passwd_line():
- def __init__(self, pwdline):
- (self.username, self.password, self.uid, self.gid, self.fullname, self.homedir, self.shell) = pwdline.strip().split(':')
- def __getitem__(self, key):
- return self.__dict__[key]
- def passwd_data4(filename):
- FILE = open(filename, "r")
- passwdfile=[y.strip() for y in FILE.readlines()]
- FILE.close()
- return [passwd_line(line) for line in passwdfile]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement