#---------------------------------------------------------
# Print days diff by Converting Unix timestamp to Readable Date/time
#---------------------------------------------------------
def convUnixTime(t):
return 1+(datetime.datetime.fromtimestamp(t*60*60*24)
- datetime.datetime.today()).days
#---------------------------------------------------------
# Read shadow file and check for account expires and create dictionary
#---------------------------------------------------------
with open( "/etc/shadow" ) as shadow:
for aLine in shadow:
filed = aLine.split(":")
f = filed[7]
try:
f = int(f)
f=convUnixTime(f)
except ValueError:
f = "NULL"
if f != "NULL" and f <= 0:
total_expired_users += 1
expr_list[ filed[0] ] = f
elif f != "NULL" and f <= min_days:
total_expring_users += 1
expr_list[ filed[0] ] = f
我已经创建了帐户已过期的用户字典,但我认为这是以更干净和简单的方式完成的。
提前致谢!!
最佳答案
使用 try-except 可能看起来更干净子句:
try:
f = int(f)
f=convUnixTime(f)
except ValueError:
pass
else:
if f <= 0:
total_expired_users += 1
expr_list[ filed[0] ] = f
elif f <= min_days:
total_expring_users += 1
expr_list[ filed[0] ] = f
您还可以稍微更改一下顺序,以避免 expr_list[filed[0]]
重复:
if f <= min_days:
expr_list[filed[0]] = f
if f <= 0:
total_expired_users += 1
else:
total_expiring_users += 1
关于Python:创建字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17079261/