# 用戶註冊 def logon(): print("歡迎來到圖書管理系統註冊頁面~") username = input("請輸入用戶名:") if len(username)<6: print("用戶名不能小於6個字符") else: email = input("請輸入郵箱:") password = input("請輸入密碼:") if len(password)<8: print("密碼不能少於8位") else: rpassword = input("請確認密碼:") if password ==rpassword: print("註冊成功!") # 函數調用,每追加一列數據都進行換行 每一個數據之間都有空格 preserve_data(path,['\n'+ username,' '+ email,' '+ password]) return True else: print("兩次輸入的密碼不一致,請從新輸入!") # 遞歸調用 logon() # 保存數據到文件 path = r'D:\Python\python-qf\文件操做\os.path\複製文件夾\案例練習\users\user.txt' def preserve_data(file_path,data): # 將字符串轉換爲bytes,由於write寫入的是字節流,不能是字符串 當爲w時須要解碼 # data = data.encode() # 打開文件,追加數據到文件 with open(file_path,'a') as wstream: # 判斷是否可寫 if wstream.writable(): wstream.writelines(data) else: print("沒有權限!") # 用戶登陸 def login(): print("歡迎來到圖書管理系統登陸頁面~") tips = input("是否已經註冊?(yes/no)") if tips =='yes': while True: username = input("輸入用戶名:") password = input("輸入密碼:") # 讀取文件時可能會出現找不到文件的異常,所以使用try except try: # 讀取文件中的內容 with open(path, 'rb') as stream: # 讀取多行保存到列表中,列表中保存的是二進制,字節 result = stream.readlines() # print(result) # 列表推導式,循環遍歷列表,將字節解碼爲字符串放在一個新列表uesr_list uesr_list = [i.decode() for i in result] # print(uesr_list) # 循環遍歷列表,檢查輸入的用戶名和密碼是否在字符串中 for i in uesr_list: # print(i) if username in i and password in i: print("登陸成功") operate(book_path,username) break else: raise Exception("用戶名或密碼錯誤,請從新輸入!") except Exception as err: print(err) # 沒有異常時執行else語句 else: break else: print("您還未註冊,請先註冊後再登陸!") # 遞歸 logon() # 查詢圖書 def find_books(path): try: with open(path, 'r') as rstream: # readlines讀到的內容是一個列表 container = rstream.readlines() # 經過列表推導式獲得新的列表,即每一個元素去掉後面的換行符號 new_container = [books_name.rstrip('\n') for books_name in container] for b_name in new_container: # 打印圖書+《》 print("《{}》".format(b_name)) except Exception as err: print("錯誤緣由:",err) #添加圖書 def add_book(b_path,username): # 添加前首先判斷是不是管理員 permission(b_path, username) # 追加圖書 不能是w ,不然會清空以前的內容 with open(b_path, 'a') as wstream: # 判斷是否可寫 if wstream.writable: msg = input("請輸入書名:") try: # 添加書籍以前判斷某本書是否已經添加 with open(b_path) as rstream: while True: line = rstream.readline() # 去掉右邊的換行 line = line.rstrip('\n') # 當找到空行是若是尚未找到與輸入的書名一致的時候,就添加輸入的書名 if not line: book = '\n' + msg wstream.write(book) print("添加成功") break else: # 輸入的圖書和讀到的行有一致的則提示不能重複添加 if line == msg: print("{}已添加,請不要重複添加哦~".format(msg)) break except Exception as err: print("錯誤緣由:", err) else: print("沒有權限") # 修改圖書 def update_book(b_path,username): permission(b_path, username) try: with open(b_path, 'r') as rstream: container = rstream.read() # 經過'\n'來分割字符串,返回結果是列表 container = container.split('\n') # print(container) # 刪除前先展現有哪些圖書 find_books(book_path) book_name = input("請輸入須要修改的圖書書名:") # 循環遍歷修改書名 for i in range(len(container)): if book_name == container[i]: rbook_name = input("請輸入修改後的圖書書名:") container[i] = rbook_name + '\n' else: # 列表中的每一個書名後面加換行符,用於寫入文件時換行 container[i] = container[i] + '\n' # print(container) # 將書名更新後的內容以writelines寫入文件中 writelines(可迭代) with open(b_path, 'w') as wwstream: wwstream.writelines(container) print("修改爲功") except Exception as err: print("錯誤緣由:", err) # 刪除圖書 def del_book(b_path,username): permission(path, username) try: with open(b_path, 'r') as rstream: container = rstream.read() # 經過'\n'來分割字符串,返回結果是列表 container = container.split('\n') # print(container) # 展現有哪些圖書 find_books(book_path) book_name = input("請輸入須要刪除的圖書書名:") # 循環遍歷修改書名 for i in range(len(container) - 1): if book_name == container[i]: container.remove(container[i]) else: # 列表中的每一個書名後面加換行符,用於寫入文件時換行 container[i] = container[i] + '\n' # print(container) # 將書名刪除後的內容以writelines寫入文件中 writelines(可迭代) with open(b_path, 'w') as wwstream: wwstream.writelines(container) print("刪除成功") except Exception as err: print("錯誤緣由:", err) # 借書 def borrow_book(username): while True: print("圖書列表:") find_books(book_path) borrow_books = input("請選擇圖書:") try: with open('../user_books/user_books.txt') as rstream: # 每次讀取一行 lines = rstream.readline() lines = lines.rstrip('\n') # 將讀到的內容經過空格分割保存到列表 lines = lines.split(' ') # 判斷輸入的書是否已被借走 if borrow_books not in lines: # print(lines) # 借書以前先判斷該用戶以前是否借過,若是借過,就在後面累加圖書,用,分割圖書 # for user_book in lines: if username in lines: with open('../user_books/user_books.txt', 'a') as wstream: # 判斷以前是否借過某本書 if borrow_books not in lines: wstream.write(' {}'.format(borrow_books)) print("借書成功") break else: print("您已借過此書,請重新選擇!") break else: # 將選擇的圖書與用戶名一塊兒保存到文件中 with open('../user_books/user_books.txt', 'a') as wstream: wstream.write('\n{} {}\n'.format(username, borrow_books)) print("借書成功") break else: print("<<{}>>已被用戶{}借走,請從新選擇~".format(borrow_books,lines[0])) except Exception as err: print("錯誤緣由:", err) # 還書 def return_book(username): try: with open('../user_books/user_books.txt') as rstream: # print("{}您已借閱,未歸還圖書以下:".format(username)) # 讀到的結果是列表 lines = rstream.readlines() # 遍歷列表,將裏面的元素再拆分爲列表 for i in range(len(lines)): # 去掉換行 lines[i] = lines[i].rstrip('\n') lines[i] = lines[i].rstrip(' ') lines[i] = lines[i].split(' ') for ii in range(len(lines[i])-1): # 只打印登陸用戶借閱的圖書 if username == lines[i][0]: print("{}您已借閱,未歸還圖書以下:".format(username)) print(lines[i][ii+1]) msg = input("請選擇你要歸還的圖書:") with open('../user_books/user_books.txt') as rstream: lines = rstream.readlines() for i in range(len(lines)): if username in lines[i] and msg in lines[i]: # 用空字符串替換msg,即表示刪除歸還的圖書 lines[i] = lines[i].replace(msg, '') with open('../user_books/user_books.txt', 'w') as wstream: # 將變動後的列表再寫入文件,只變動當前用戶的圖書信息 wstream.writelines(lines) print("歸還成功!") break with open('../user_books/user_books.txt') as rstream: lines = rstream.readlines() for i in range(len(lines)): lines[i] = lines[i].rstrip('\n') lines[i] = lines[i].rstrip(' ') lines[i] = lines[i].split(' ') # print(type(lines[i])) for ii in range(len(lines[i])): # 圖書歸還成功後判斷列表中只有用戶名了,若是隻有用戶名則將用戶名用空字符串代替 if username == lines[i][0] and len(lines[i]) == 1: lines[i][0] = lines[i][0].replace(lines[i][0], '') lines.append(lines[i][0]) # print(lines) str = '' for i in range(len(lines)): for ii in range(len(lines[i])): # 將嵌套列表中的元素取出來拼接成字符串 str += lines[i][ii] + ' ' str += '\n' # 遍歷完畢刪除以前列表裏面嵌套的列表,追加字符串str lines.clear() lines.append(str) # print(lines) # 將更新後的列表寫入文件 with open('../user_books/user_books.txt', 'w') as wstream: wstream.writelines(lines) else: print("您尚未借閱記錄哦~") except Exception as err: print("錯誤緣由:", err) # 查看我的信息 def look_person_info(path,username): with open(path) as rstream: while True: line = rstream.readline() if not line: break else: line = line.split(' ') # 列表推導式 line = [info.strip('\n') for info in line] if username in line: print("----我的信息----") print("用戶名:",line[0]) print("郵箱:", line[1]) print("密碼:", line[2]) # 修改我的信息 def update_password(path,username): tips = input("請選擇操做:\n 1.修改郵箱\n 2.修改密碼\n") # 修改郵箱 if tips =='1': new_email = '' line = [] try: with open(path) as rstream: while True: line = rstream.readline() if not line: break line = line.split(' ') # 去掉密碼後面的換行符 line[2] = line[2].rstrip('\n') if username == line[0]: new_email = input("請輸入新郵箱:") line[1] = new_email break except Exception as err: print(err) else: # 將新修改郵箱後的用戶的全部信息追加到文件夾 with open(path, 'a') as wstream: for i in range(len(line)): if i == 0: # 遍歷列表,第一個列表元素須要前面須要加換行,後面須要加空格與其餘元素分割 line[i] = '\n' + line[i] + ' ' else: line[i] = line[i] + ' ' wstream.writelines(line) print("修改爲功") # 刪除修改郵箱以前用戶的信息 with open(path) as rstream: # 讀取多行 lines = rstream.readlines() i = 0 l = len(lines) while i < l: # 當 當前用戶名在用戶信息行且新的郵箱不在時就刪除以前的用戶信息,不會刪除其餘用戶的信息 if username in lines[i] and new_email not in lines[i]: lines.remove(lines[i]) i += 1 l -= 1 # 刪除舊郵箱對應的當前用戶信息後,再將新郵箱對應的用戶信息以及其餘用戶的信息重新寫入到文件 with open(path, 'w') as wstream: wstream.writelines(lines) # 修改密碼 elif tips =='2': new_password = '' line = [] try: with open(path) as rstream: while True: line = rstream.readline() if not line: break line = line.split(' ') # 去掉密碼後面的換行符 line[2] = line[2].rstrip('\n') if username == line[0]: new_password = input("請輸入新密碼:") # 判斷新密碼與舊密碼是否一致 if new_password ==line[2]: # 拋出異常 raise Exception("新密碼不能與舊密碼相同哦~") else: line[2] =new_password break # 能夠捕獲到前面raise拋出的異常 except Exception as err: print(err) else: # 將新修改密碼後的用戶的全部信息追加到文件夾 with open(path,'a') as wstream: for i in range(len(line)): if i ==0: # 遍歷列表,第一個列表元素須要前面須要加換行,後面須要加空格與其餘元素分割 line[i] = '\n'+line[i]+' ' else: line[i] = line[i] +' ' wstream.writelines(line) print("修改爲功") # 刪除修改密碼以前用戶的信息 with open(path) as rstream: # 讀取多行 lines = rstream.readlines() i =0 l = len(lines) while i < l: # 當 當前用戶名在用戶信息行且新的密碼不在時就刪除以前的用戶信息,不會刪除其餘用戶的信息 if username in lines[i] and new_password not in lines[i]: lines.remove(lines[i]) i+=1 l-=1 # 刪除舊密碼對應的當前用戶信息後,再將新密碼對應的用戶信息以及其餘用戶的信息重新寫入到文件 with open(path,'w') as wstream: wstream.writelines(lines) # 我的信息 def person_information(path,username): tips = input("請選擇操做:\n 1.查看我的信息\n 2.修改我的信息\n") if tips =='1': look_person_info(path,username) elif tips =='2': update_password(path, username) # 只有管理員才能夠進行圖書的增刪改操做 def permission(user_path,username): try: with open(user_path) as rstream: while True: line = rstream.readline() # 讀到空行跳出循環 if not line: break # 經過3個空格將字符串line分割爲一個列表,存儲三個值 line = line.split(' ') # 循環遍歷列表,去掉列表中每一個元素後面的換行,若是有就去掉,沒有就不取掉 for i in range(len(line)): line[i] = line[i].rstrip('\n') # 判斷是否管理員,若是是就能夠進行添加操做 if username == 'admin123': pass else: print("只有管理員才能夠進行該操做~".format(username)) # 不是管理員將回到操做頁面 operate(path, username) except Exception as err: print("錯誤緣由:",err) # 圖書增刪改借還操做 book_path = '../books/books.txt' # book_list = ['水滸傳\n','紅樓夢\n','廊橋遺夢'] def operate(b_path,username): while True: msg = input("請選擇操做:\n 1.查詢圖書\n 2.添加圖書\n 3.修改圖書\n 4.刪除圖書\n 5.借書\n 6.歸還圖書\n 7.我的信息\n 8.退出登陸\n") # 查詢圖書 if msg =='1': find_books(book_path) # 添加圖書 elif msg =='2': add_book(b_path, username) # 修改圖書 elif msg =='3': update_book(b_path, username) # 刪除圖書 elif msg =='4': del_book(b_path, username) # 借書 elif msg =='5': borrow_book(username) # 還書 elif msg =='6': return_book(username) #查看、修改我的信息 elif msg =='7': person_information(path, username) # 退出登陸 elif msg =='8': msg = input("肯定退出登陸嗎?(yes/no)") if msg == 'yes': break login()