python利用文件讀寫編寫一個(gè)博客
本文實(shí)例為大家分享了python利用文件讀寫編寫一個(gè)博客的具體代碼,供大家參考,具體內(nèi)容如下
代碼展示
import random
import json
import time
import os
def zhuce():
print("*********************正在注冊*********************")
try:
users = readfile()
except:
fa = open(r'test.json', "w",encoding="utf-8")
fa.write(json.dumps({"初始化": "初始化"}))
fa.close()
users = readfile()
os.makedirs('用戶信息')
b = 0
user_name = input("請輸入你的用戶名:")
user_password = input("請輸入你的密碼:")
for key in users.keys():
if user_name == key:
print("用戶名已存在")
b = 1
break
if b == 0:
writefile1(user_name)
users[user_name] = user_password
writefile1(user_name)
writefile(users)
return b
def readfile():
f = open(r'test.json', "r+")
f1 = json.load(f)
f.close()
return f1
def writefile(a):
a = json.dumps(a)
f = open(r'test.json', "w")
f.write(a)
f.close()
def homepage():
print("*********************微博主頁*********************")
a = input("1注冊 2登錄 3退出\n請輸入你的選擇:")
return a
def zhucetexiao():
print("注冊中,請稍后!")
print("更新成功!")
print("注冊成功!")
print("默認(rèn)文章表創(chuàng)建完成!")
def denglu():
b = 0
print("*********************正在登錄*********************")
users = readfile()
user_name = input("登錄用戶名:")
user_password = input("登錄密碼:")
for key in users.keys():
if user_name == key:
print("用戶存在,判斷密碼")
b = 2
if user_password == users[key]:
print("登陸成功")
b = 1
dengluxiaoguo(user_name)
break
if b == 0:
print("登錄失敗")
return b
def dengluxiaoguo(user_name):
while 0 == 0:
a = input("1寫文章 2讀取所有 3讀取一篇 4編輯一篇 5刪除一篇 6登出\n請輸入你的選擇:")
users = article_read(user_name)
if a == "1":
print("*********************寫一篇文章*********************")
article_write(user_name)
elif a == "2":
duqusuoyou(user_name)
elif a == "3":
print("*********************讀一篇文章*********************")
duquyipian(user_name)
elif a == "4":
print("*********************改一篇文章*********************")
bianjiyipian(user_name)
elif a == "5":
print("*********************刪一篇文章*********************")
shanchuyipian(user_name)
elif a == "6":
break
def shanchuyipian(user_name):
duqusuoyou(user_name)
a = input("請輸入您要?jiǎng)h除的序號(hào)?")
f = open(f'用戶信息\{user_name}.json', "r+")
f1 = json.load(f)
f.close()
if len(f1) > 1:
if int(a) > 0 and int(a) <= len(f1):
f1.pop(a)
for i in range(1, len(f1) + 2):
if i > int(a):
f1[str(i - 1)] = f1.pop(str(i))
f = open(f'用戶信息\{user_name}.json', "w")
b = json.dumps(f1)
f.write(b)
f.close()
else:
print("未找到該序列")
else:
print("最后一篇文章拒絕刪除")
duqusuoyou(user_name)
def bianjiyipian(user_name):
duqusuoyou(user_name)
a = input("請輸入您要編輯的序號(hào)?")
f = open(f'用戶信息\{user_name}.json', "r+")
f1 = json.load(f)
f.close()
if int(a) > 0 and int(a) <= len(f1):
article_name = input("文章名稱")
article_content = input("文章內(nèi)容")
f = open(f'用戶信息\{user_name}.json', "r+")
f1 = json.load(f)
f.close()
f = open(f'用戶信息\{user_name}.json', "w")
time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
f1[a] = {"title": article_name, "time": time1, "author": user_name, "content": article_content}
b = json.dumps(f1)
f.write(b)
f.close()
else:
print("未找到該序列")
def duquyipian(user_name):
duqusuoyou(user_name)
a = input("請輸入您要查看的序號(hào)?")
f = open(f'用戶信息\{user_name}.json', "r+")
f1 = json.load(f)
f.close()
if int(a) > 0 and int(a) <= len(f1):
print(f1[a])
else:
print("未找到該序列")
def duqusuoyou(user_name):
print("*********************文章目錄*********************")
f = open(f'用戶信息\{user_name}.json', "r+")
f1 = json.load(f)
f.close()
for key in f1.keys():
print(f"{key} {f1[key]['title']} {f1[key]['time']}")
def article_read(user_name):
f = open(f'用戶信息\{user_name}.json', "r+")
f1 = json.load(f)
f.close()
return f1
def article_write(user_name):
f = open(f'用戶信息\{user_name}.json', "r+")
f1 = json.load(f)
f.close()
key_count = len(f1) + 1
article_name = input("文章名稱:")
article_content = input("文章內(nèi)容:")
time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
f = open(f'用戶信息\{user_name}.json', "w")
# print(type(key_count))
f1[str(key_count)] = {"title": article_name, "time": time1, "author": user_name, "content": article_content}
print(f1)
b = json.dumps(f1)
f.write(b)
f.close()
def readfile1():
f = open(r'test1.json', "r+")
f1 = json.load(f)
f.close()
return f1
def writefile1(a):
time1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
f = open(f'用戶信息\{a}.json', "w")
b = {1: {"title": "初始化文章", "time": time1, "author": "0", "content": "0"}}
print(b)
b = json.dumps(b)
f.write(b)
f.close()
while 0 == 0:
h = homepage()
if h == "1":
if zhuce() == 0:
zhucetexiao()
elif h == "2":
if denglu() == 1:
pass
else:
exit()
文件保存樣式:

運(yùn)行結(jié)果:



以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python數(shù)據(jù)解析之BeautifulSoup4的用法詳解
Beautiful?Soup?是一個(gè)可以從?HTML?或?XML?文件中提取數(shù)據(jù)的?Python?庫,這篇文章主要來和大家介紹一下BeautifulSoup4的用法,需要的可以參考一下2023-06-06
python 3利用BeautifulSoup抓取div標(biāo)簽的方法示例
這篇文章主要介紹了python 3利用BeautifulSoup抓取div標(biāo)簽的方法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-05-05
使用keras實(shí)現(xiàn)densenet和Xception的模型融合
這篇文章主要介紹了使用keras實(shí)現(xiàn)densenet和Xception的模型融合,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python搭建Keras CNN模型破解網(wǎng)站驗(yàn)證碼的實(shí)現(xiàn)
這篇文章主要介紹了Python搭建Keras CNN模型破解網(wǎng)站驗(yàn)證碼的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Python實(shí)現(xiàn)獲取郵箱內(nèi)容并解析的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)獲取郵箱內(nèi)容并解析的方法,結(jié)合完整實(shí)例形式分析了Python登陸pop3服務(wù)器并解析獲取郵箱內(nèi)容相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
Python實(shí)現(xiàn)員工信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)員工信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Python爬蟲之獲取心知天氣API實(shí)時(shí)天氣數(shù)據(jù)并彈窗提醒
今天我們來學(xué)習(xí)如何獲取心知天氣API實(shí)時(shí)天氣數(shù)據(jù),制作彈窗提醒,并設(shè)置成自啟動(dòng)項(xiàng)目.文中有非常詳細(xì)的代碼示例及介紹,對正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05
Python開發(fā)工具Pycharm的安裝以及使用步驟總結(jié)
今天給大家?guī)淼氖顷P(guān)于Python開發(fā)工具的安裝以及使用的相關(guān)知識(shí),文章圍繞著如何使用和安裝Pycharm展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06

