擅长:python、mysql、java
<p>有几种方法可以读取和解析输入文件。假设您的文件是您指定的方式,并且您愿意处理异常,下面是一个示例方法。您需要适当地处理异常。你知道吗</p>
<pre><code>try:
#This will read your in.conf which contains user/pwds in the dictionary format you specified.
#Read documentation on exec here:
# https://docs.python.org/3.0/library/functions.html#exec
with open('in.conf') as fh:
for line in fh:
exec(line)
user_id = input("User ID:\n")
user_password = input('Password:\n')
if user_id in managers and user_password == managers[user_id]:
print("Welcome, {0}.".format (user_id))
user_status='manager'
elif user_id in employees and user_password == employees[user_id]:
print("Welcome, {0}.".format (user_id))
user_status='staff'
else:
print "invalid credentials"
except NameError:
#catch situations where your file doesn't contain managers or employees dictionary
#I just raise it so you can see what it would print
raise
except:
#other exceptions as you see appropriate to handle ....
#I just raise it so you can see what it would print
raise
</code></pre>