提问人:phldlphegls1 提问时间:8/9/2023 更新时间:8/9/2023 访问量:33
如何覆盖txt文件名中的信息已经存在
How to overwrite info in txt file name already exists
问:
我有一个程序,可以为您放置的任何网站创建一个随机生成的密码。如果可能的话,我想要的是,如果用户输入Amazon并输出密码,然后再次输入Amazon,它不会在文本文件中复制Amazon,而是使用新密码更新以前输入的密码。我不确定这是否可能,或者它是否总是附加到我的列表中。
import random
import string
# name of website we're creating the password for
website = input("What website are you creating a password for? ").capitalize()
def char_length():
# makes it so this part will repeat
while True:
length = input("How many characters would you like your password to be? ")
if length.isdigit() and 0 < int(length) < 51:
return int(length)
else:
print('Response must be a number between 0 and 50')
def random_pass(length):
while True:
spec_char = input("Would you like special characters in your password y/n? ").lower()
if spec_char == "y":
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
print("Your password for", website, "is:", password)
return password
elif spec_char == "n":
characters = ''.join(random.choice(string.ascii_letters) for i in range(length))
print("Your password for", website, "is:", characters)
return characters
else:
print("Please only type 'Yes' or 'No'.")
random_pass(char_length())
file = open("password.txt", "a+")
webname = file.write(website)
file.write(": ")
new_pass = file.write(random_pass(char_length()))
file.write("\n")
print(webname)
print(new_pass)
file.close()
我自己尝试过几种不同的方式,但我只学习了一个月 python,所以我不知道该如何解决这个问题。
答: 暂无答案
评论
json.load
if website in data:
json.dump
json.dump
json.load