import sys
count =0
#f_name = "D:/22. Test/test.txt"
f_name = sys.argv[3]
f_new_name ="%s.new"%f_name
old_str =str(sys.argv[1])
new_str =str(sys.argv[2])
f =open(f_name,'r')
f_new =open(f_new_name,'w')
for linein f:
if old_strin line:
new_line = line.replace(old_str,new_str)
count +=1
else:
new_line = line
f_new.write(new_line)
print("Changing from {0} to {1} ...".format(old_str,new_str))
print("File {0} have replaced {1} positions!".format(f_name,count))
f.close()
f_new.close()
#os.rename(f_new_name,f_name)
userName = ("zhangsan", "lisi", "wangwu", "zhaoliu", "xiaolin", "xiaohua")
pwList = ['123', '456', '789', '246', 'abc', 'abc1234']
def exist1():
for iin range(3):
username =input("请输入用户名:")
if usernamenot in userName:
print('用户不存在')
else:
return userName.index(username)
print("用户名输入超过3次")
return -1
def exist2(a):
if a == -1:
return 0
for iin range(3):
password =input("请输入密码:")
if pwList[a] == password:
print("登陆成功")
return 0
else:
print("密码不正确")
print("密码输入超过3次")
return 0
def regist():
exist2(exist1())
regist()
def check_pass(username, password):
with open('userfile.txt', 'r+')as f:
content = f.readlines()
for iin content:
if i.split(',')[0] == usernameand i.split(',')[1].strip('\n') == password:
return True
break
else:
return False
#判断用户名是否锁定:
def isLock(username):
with open('locklist.txt', 'r')as f:
cont = f.read()
if usernamein cont:
return False
else:
return True
#写锁定用户名到locklist,如果用户输错三次,就调用此函数,讲锁定的用户名写入此文件
def writeErrlist(username):
with open('locklist.txt', 'a+')as f:
f.write(username)
f.write('\n')
def main():
count =0 #存储输密码次数
while count <3:
username =input('Please input your username: ')
is_lock = isLock(username)
if is_lock:
passwd =input('Please input your password: ')
result = check_pass(username, passwd)
if result:
print('welcome back! "{}"'.format(username))
break
else:
count +=1
if count <3:
print('Invalid username or password, Please try again!')
else:
print('Too many attempts, Your account has benn locked.')
writeErrlist(username)
print('Your account has been locked!!!')
if __name__ =="__main__":
main()
https://blog.csdn.net/weixin_42444693/article/details/109212551
网友评论