a = "dba abc\n"
print (a.strip())
print (a.split(" "))
结果:
dba abc
['dba', 'abc\n']
def load(self):
"""
Load /etc/passwd
"""
self.passwd = []
with open(self.passwd_file, 'r') as f:
while True:
rawline = f.readline()
if not rawline:
break
line = rawline.strip()
if not line:
continue
if line.startswith('#'):
continue
(pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir,
pw_shell) = line.split(':')
e = {}
e["pw_name"] = pw_name
e["pw_passwd"] = pw_passwd
e["pw_gecos"] = pw_gecos
e["pw_dir"] = pw_dir
e["pw_shell"] = pw_shell
try:
e["pw_uid"] = int(pw_uid)
except ValueError:
e["pw_uid"] = 1001
try:
e["pw_gid"] = int(pw_gid)
except ValueError:
e["pw_gid"] = 1001
self.passwd.append(e)