Python学習: Day 31Capstone Project
2024年3月末までの目標🌕!
- Udemyの100 Days of Codeを完了
- おまけに欲張ってCISSP資格を取得し、Cyber Security分野に転向
私の使ってる学習素材です📚。アンジェラねーさんには足向けて寝れませんッ。
今日のPlaylist:
あれ?書いたつもりだったけど、消えてる😨。
import json
# ---------------------------- SAVE PASSWORD ------------------------------- #
def save():
website = website_entry.get()
email = email_entry.get()
password = password_entry.get()
new_data = {
website: {
"email": email,
"password": password,
}
}
if len(website) == 0 or len(password) == 0:
messagebox.showinfo(title="Oops", message="Please make sure you haven't left any fields empty.")
else:
try:
with open("data.json", "r") as data_file:
#Reading old data
data = json.load(data_file)
except FileNotFoundError:
with open("data.json", "w") as data_file:
json.dump(new_data, data_file, indent=4)
else:
#Updating old data with new data
data.update(new_data)
with open("data.json", "w") as data_file:
#Saving updated data
json.dump(data, data_file, indent=4)
finally:
website_entry.delete(0, END)
password_entry.delete(0, END)
# ---------------------------- FIND PASSWORD ------------------------------- #
def find_password():
website = website_entry.get()
try:
with open("data.json") as data_file:
data = json.load(data_file)
except FileNotFoundError:
messagebox.showinfo(title="Error", message="No Data File Found.")
else:
if website in data:
email = data[website]["email"]
password = data[website]["password"]
messagebox.showinfo(title=website, message=f"Email: {email}\nPassword: {password}")
else:
messagebox.showinfo(title="Error", message=f"No details for {website} exists.")
# Buttons
search_button = Button(text="Search", width=13, command=find_password)
search_button.grid(row=1, column=2)
generate_password_button = Button(text="Generate Password", command=generate_password)
generate_password_button.grid(row=3, column=2)
add_button = Button(text="Add", width=36, command=save)
add_button.grid(row=4, column=1, columnspan=2)
window.mainloop()
まずは
else:
try:
with open("data.json", "r") as data_file:
#Reading old data
data = json.load(data_file)
これでJsonファイルを作り、その中にあるdataを読む。
except FileNotFoundError:
with open("data.json", "w") as data_file:
json.dump(new_data, data_file, indent=4)
でもファイルがなかったらdata.jsonを作って書き込む。
else:
#Updating old data with new data
data.update(new_data)
そのほかの場合は、Updateだね。
with open("data.json", "w") as data_file:
#Saving updated data
json.dump(data, data_file, indent=4)
ファイルを開き、Save。
そしてSearchボタンを作って終了。
この記事が気に入ったらサポートをしてみませんか?