python應(yīng)用文件讀取與登錄注冊功能
更新時間:2019年09月23日 10:48:05 作者:紅桃五
這篇文章主要介紹了python應(yīng)用文件讀取寫登錄注冊功能,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
python應(yīng)用文件讀取與登錄注冊功能,具體實現(xiàn)代碼如下所示:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: zhw
#讀取文件中的內(nèi)容
def open_file(filename ,file_type , *args):
#讀取文件中的數(shù)據(jù)
if file_type == 'r':
flie_list = []
with open(filename,file_type) as sub_all:
for file_line in sub_all:
flie_list.append(file_line.strip())
#print(flie_list)
return flie_list
#讀取文件中的用戶號
elif file_type == 'ru':
flie_list = []
with open(filename,'r') as sub_all:
for file_line in sub_all:
file_l = file_line.split('|')
user_no = file_l[0]
flie_list.append(user_no.strip())
#print(flie_list)
return flie_list
#寫入文件
elif file_type == 'a+':
data = args[0]
with open(filename, file_type) as sub_all:
sub_all.write(data +"\n")
else:
print('您輸入的格式?jīng)]有,請檢查')
class User(object):
def __init__(self, user_no, user_pw):
self.user_no = user_no
self.user_pw = user_pw
self.file = 'login.log'
@property
#檢查賬號是否已經(jīng)存在
def check_user(self):
flie_list = open_file(self.file, 'ru')
if self.user_no in flie_list:
return '1'
else:
return '0'
#注冊賬號
def regist(self):
user_info = '%s|%s' % (self.user_no, self.user_pw)
open_file(self.file, 'a+',user_info)
#return'注冊成功'
#賬號登錄
def login(self):
flie_list = open_file(self.file, 'r')
user_info = '%s|%s' % (self.user_no, self.user_pw)
if user_info in flie_list:
return '1'
else:
return '0'
def return_more(args):
pass
#循環(huán)驗證登錄,循環(huán)注冊賬號
while True:
print('請選擇\n '
'1. 注冊\n'
'2. 登錄\n'
'3. 退出')
choose = input('請選擇').strip()
if choose == '1':
for i in range(3):
user_no = input('請輸入注冊賬號:').strip()
user_pw = ''
user = User(user_no,user_pw)
#檢查用戶號是否存在
return_more = user.check_user
if return_more == '0':
user_pw = input('請輸入注冊密碼:').strip()
user = User(user_no, user_pw)
return_more = user.regist()
print(return_more)
break
else:
print('您輸入的賬號已經(jīng)存在,請選擇其他賬號')
#print('您輸入的錯誤次數(shù)太多,已退出!')
elif choose == '2':
for i in range(3):
user_no = input('請輸入登錄賬號:').strip()
user_pw = input('請輸入登錄密碼').strip()
user = User(user_no, user_pw)
#用戶登錄
return_mk = user.login()
if return_mk =='0':
print("登錄失敗,請重新輸入")
else:
print('登錄成功')
break
elif choose == '3':
break
else:
print('輸入序號有無,請重新輸入')
總結(jié)
以上所述是小編給大家介紹的python應(yīng)用文件讀取與登錄注冊功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
您可能感興趣的文章:
相關(guān)文章
Python調(diào)用JavaScript代碼的幾種方法小結(jié)
日常Web端爬蟲過程中,經(jīng)常會遇到參數(shù)被加密的場景,因此,我們需要分析網(wǎng)頁源代碼通過調(diào)式,一層層剝離出關(guān)鍵的JS代碼,使用Python去執(zhí)行這段代碼,本文將聊聊利用 Python 調(diào)用 JS 的4種方式,需要的朋友可以參考下2024-12-12
利用Pandas讀取文件路徑或文件名稱包含中文的csv文件方法
今天小編就為大家分享一篇利用Pandas讀取文件路徑或文件名稱包含中文的csv文件方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07

