from datetime import datetime
import os
#保存信息路徑
def add_path():
path1=r"G:\zhanghua\321.txt"
path2=r"G:\zhanghua\文章信息.txt"
parent_path1=os.path.dirname(path1)
parent_path2=os.path.dirname(path2)
if not os.path.exists(parent_path1):
os.makedirs(parent_path1)
if not os.path.exists(parent_path2):
os.makedirs(parent_path2)
file1=open(path1,"w",encoding="utf-8")
file2=open(path2,"w",encoding="utf-8")
file1.write("{}")
file2.write("[]")
file1.close()
file2.close()
return True
#保存用戶信息
def add_user(userdict):
path=r"G:\zhanghua\321.txt"
file=open(path,"w",encoding="utf-8")
file.write(str(userdict))
file.close()
return True
#讀取用戶信息
def read_user():
path=r"G:\zhanghua\321.txt"
file=open(path,"r",encoding="utf-8")
user_dict=file.read()
file.close()
return eval(user_dict)
#用戶名規(guī)范
def re_username(username):
a=True
if len(username)>=6 and username.isdigit():
pass
else:
a=False
return a
#判斷用戶是否存在
def is_user_name(username):
user_dict=read_user()
if username in user_dict:
return False
return True
#添加用戶
#(第二次保存用戶信息)本次寫入會(huì)覆蓋寫入的內(nèi)容
def add_users(username,password):
user_dict=read_user()
user_dict[username]=password
add_user(user_dict)
user_dict=read_user()
return True
#判斷用戶名和密碼是否存在
def is_username_and_password(username,password):
user_dict=read_user()
if user_dict.get(username)==password:
return True
return False
#密碼規(guī)范
def re_password(password):
b=True
if len(password)>=8:
for pwd in password:
s="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
if pwd not in s:
b=False
if len(set(password))==1:
b=False
if password.isdigit() or password.isalpha():
b=False
print("密碼不可以是純數(shù)字或純字母")
else:
b=False
print("密碼長度不得低于8位")
return b
#保存文章信息
def add_article(article_news):
path=r"G:\zhanghua\文章信息.txt"
file=open(path,"w",encoding="utf-8")
file.write(str(article_news))
file.close()
return True
#讀取文章信息
def read_article():
path=r"G:\zhanghua\文章信息.txt"
file=open(path,"r",encoding="utf-8")
article_dict_list=file.read()
file.close()
return eval(article_dict_list)
#判斷文章編號(hào)是否存在
def is_article_id(article_id):
article_dict_list=read_article()
for article_dict in article_dict_list:
if article_dict.get("art_id")==article_id:
return True
return False
#添加文章
def add_articles(article_dict):
article_dict_list=read_article()
article_dict_list.append(article_dict)
add_article(article_dict_list)
return True
#根據(jù)編號(hào)查看文章
def read_id_article(article_id):
article_dict_list=read_article()
for article_dict in article_dict_list:
if article_dict.get("art_id")==article_id:
return article_dict
return None
#根據(jù)作者查看文章
def read_author_article(article_author):
article_dict_list=read_article()
for article_dict in article_dict_list:
if article_dict.get("art_author")==article_author:
return article_dict
return None
#繼續(xù)查看該作者文章
def re_read_author_article(article_author):
article_dict_list=read_article()
ls=[]
for article_dict in article_dict_list:
if article_dict.get("art_author")==article_author:
ls.append(article_dict)
return ls
#查看所有文章
def read_all_article():
article_dict_list=read_article()
if article_dict_list!=[]:
for article_dict in article_dict_list:
print("文章編號(hào):",article_dict.get("art_id"))
print("文章作者:",article_dict.get("art_author"))
print("文章標(biāo)題:",article_dict.get("art_title"))
print("文章內(nèi)容:",article_dict.get("art_cent"))
print("文章時(shí)間:",article_dict.get("art_time"))
else:
print("沒有文章")
return True
#根據(jù)編號(hào)刪除文章
def delate_id_article(article_id):
article_dict_list=read_article()
for article_dict in article_dict_list:
if article_dict.get("art_id")==article_id:
article_dict_list.remove(article_dict)
add_article(article_dict_list)
return True
return False
#根據(jù)作者刪除文章
def delate_author_article(article_author):
article_dict_list=read_article()
for article_dict in article_dict_list:
if article_dict.get("art_author")==article_author:
article_dict_list.remove(article_dict)
add_article(article_dict_list)
return True
return False
#繼續(xù)刪除文章
def re_delate_author_article(article_author):
article_dict_list=read_article()
ls=[]
for article_dict in article_dict_list:
if article_dict.get("art_author")==article_author:
ls.append(article_dict)
for i in ls:
article_dict_list.remove(i)
add_article(article_dict_list)
return True
#刪除所有文章
def delate_all_article():
article_dict_list=read_article()
article_dict_list.clear()
add_article(article_dict_list)
return True
def main():
while True:
p=input("請選擇功能:1.注冊2.登陸3.退出")
if p=="1":
username=input("請輸入用戶名:")
bb=re_username(username)#用戶名是否符合規(guī)范
if bb==True:
a=is_user_name(username)#用戶名是否存在
if a==True:
password=input("用戶名可用,請輸入密碼:")
cc=re_password(password)#密碼是否符合規(guī)范
if cc==True:
b=add_users(username,password)
if b==True:
print("注冊成功")
else:
print("注冊失敗")
else:
print("密碼只能是數(shù)字和字母或下劃線組成")
else:
print("用戶名已存在")
else:
print("用戶名不可用")
elif p=="2":
username2=input("請輸入用戶名:")
password2=input("請輸入密碼:")
c=is_username_and_password(username2,password2)
if c==True:
print("歡迎你%s登陸成功"%username2)
while True:
pp=input("請選擇功能1.發(fā)表文章2.查看文章3.刪除文章4.返回上一級(jí)")
if pp=="1":
article_id=input("請輸入文章編號(hào):")
ax=is_article_id(article_id)
if ax==False:
article_title=input("請輸入文章標(biāo)題:")
article_author=input("請輸入文章作者:")
article_cent=input("請輸入文章內(nèi)容:")
article_time=datetime.now().strftime("%Y{y}%m{m}%dmoiyehiw".format(y="年",m="月",d="日"))
s=add_articles({"art_id":article_id,"art_title":article_title,"art_author":article_author,"art_cent":article_cent,"art_time":article_time})
if s==True:
print("文章發(fā)表成功")
else:
print("文章發(fā)表失敗")
else:
print("文章編號(hào)已經(jīng)存在,請重新輸入")
elif pp=="2":
while True:
pl=input("請選擇功能:1.編號(hào)查看文章2.作者查看文章3.查看所有文章4.返回上一級(jí)")
if pl=="1":
article_id=input("請輸入文章編號(hào):")
d=read_id_article(article_id)
if d!=None:
print("文章編號(hào):",d.get("art_id"))
print("文章作者:",d.get("art_author"))
print("文章標(biāo)題:",d.get("art_title"))
print("文章內(nèi)容:",d.get("art_cent"))
print("文章時(shí)間:",d.get("art_time"))
else:
print("該文章編號(hào)不存在")
elif pl=="2":
author=input("請輸入作者:")
e=read_author_article(author)
if e!=None:
print("文章編號(hào):",e.get("art_id"))
print("文章作者:",e.get("art_author"))
print("文章標(biāo)題:",e.get("art_title"))
print("文章內(nèi)容:",e.get("art_cent"))
print("文章時(shí)間:",e.get("art_time"))
while True:
pw=input("請選擇功能:1.繼續(xù)查詢2.返回上一級(jí)")
if pw=="1":
print("繼續(xù)查詢")
f=re_read_author_article(author)
if f!=None:
if len(f)==1:
print("該作者沒有更多作品了")
for i in range(1,len(f)):
print("文章編號(hào):",f[i].get("art_id"))
print("文章作者:",f[i].get("art_author"))
print("文章標(biāo)題:",f[i].get("art_title"))
print("文章內(nèi)容:",f[i].get("art_cent"))
print("文章時(shí)間:",f[i].get("art_time"))
else:
print("該作者沒有發(fā)表作品了")
elif pw=="2":
break
else:
print("暫未開通該功能")
else:
print("該%s作者還沒有作品"%article_author)
elif pl=="3":
m=read_all_article()
if m==True:
...
else:
print("還沒有作品可以展示")
elif pl=="4":
break
else:
print("暫未開通該功能")
elif pp=="3":
while True:
px=input("請選擇功能:1.根據(jù)編號(hào)刪除文章2.根據(jù)作者刪除文章3.刪除所有文章4.返回上一級(jí)")
if px=="1":
article_id=input("請輸入文章編號(hào)")
f=delate_id_article(article_id)
if f==True:
print("%s刪除成功"%article_id)
else:
print("該文章不存在或者刪除失敗")
elif px=="2":
article_author=input("請輸入作者")
g=delate_author_article(article_author)
if g==True:
print("刪除成功")
while True:
pd=input("請選擇功能:1.繼續(xù)刪除2.返回上一級(jí)")
if pd=="1":
h=re_delate_author_article(article_author)
if h==True:
print("刪除成功")
else:
print("該作者沒有更多作品了")
elif pd=="2":
break
else:
print("該功能暫時(shí)未開通")
else:
print("該%作者還沒有作品刪除失敗"%article_author)
elif px=="3":
l=delate_all_article()
if l==True:
print("刪除成功")
else:
print("刪除失敗")
elif px=="4":
break
print("返回上一級(jí)")
else:
print("該功能暫未開通")
elif pp=="4":
print("返回上一級(jí)")
break
else:
print("暫未開通該功能")
else:
print("賬號(hào)不存在或者密碼輸入錯(cuò)誤")
elif p=="3":
break
else:
print("暫未開通該功能")
def main1():
path1=r"G:\zhanghua\321.txt"
path2=r"G:\zhanghua\文章信息.txt"
if os.path.exists(path1) and os.path.exists(path2):
pass
else:
add_path()
main()
main1()