У меня проблемы с удалением учетной записи в текстовом файле , хотя это школьная работа.
Вот мой код (длинный и сложный):
create = ""
import time
import sys
while True:
print("Type create to create, delete to delete")
acc = input("an account or quit to exit Python: ")
while acc.lower() == "create":
found = False
MinName = 5
MinPass = 8
print("Please enter your name.")
create = input(">").lower()
if len(create) < MinName:
print("The name is too short.")
print("Please enter your name.")
create = input(">").lower()
# Open a file
fi = open("E:/New folder/NO/How about no/accounts.txt", "r")
#get the data
data = fi.readlines()
# Close opened file
fi.close()
#check each line in the text file
for li in data:
#if the name is already in a file
if(li.split(":")[0].lower() == create):
#print an error of similar username
print("Found a similar name, try a different one!")
#the name has been found
found = True
#if the name wasn't found anywhere in the file
while found == False:
#ask the user for the password
passk = input("Your name is OK, enter pasword:\n>")
if len(passk) < MinPass:
print("Password must be a minimum of 8 letters.")
passk = input(">")
# Open a file to append to
fi = open("E:/New folder/NO/How about no/accounts.txt", "a")
#add the new name/password to the file
fi.write("\n" + create + ":" + passk)
# Close opened file
fi.close()
print("Created an account. You may now login.")
sys.exit()
#Deleting an account##########
while acc.lower() == 'delete':
MinName = 5
MinPass = 8
print("Please enter your name.")
delete = input(">").lower()
if len(delete) < MinName:
print("The name is too short.")
print("Please enter your name.")
delete = input(">").lower()
fi = open("E:/New folder/NO/How about no/accounts.txt", "r")
data = fi.readlines()
fi.close()
#check each line in the text file
for li in data:
#if the name is in a file
if(li.split(":")[0].lower() == delete):
#print an text
print("Found the name.")
#the name has been found
found = True
#Password
while True:
#Set foundpass to false
foundpass = False
print("Enter your password:")
del2 = input(">").lower()
if len(del2) < MinPass:
print("The password is too short.")
print("Please enter your name.")
delete = input(">").lower()
fi = open("E:/New folder/NO/How about no/accounts.txt", "r")
data = fi.readlines()
fi.close()
for li in data:
if(li.split(":")[1].lower() == del2):
print("Are you sure you want to delete this account?")
delok = input("You may lose all your data (for a long time)! Y/N\n>")
if delok.lower() == 'y':
file = open("E:/New folder/NO/How about no/accounts.txt", "r")
lines = f.readlines()
f.close()
file = open("E:/New folder/NO/How about no/accounts.txt", "w")
for line in lines:
if line !=
sys.exit()
elif delok.lower() == 'n':
print("Your account is not deleted.")
sys.exit()
if foundpass == False:
print("Incorrect password!")
#If the name is not in the file
if found == False:
print("Incorrect name!")
delete = input(">").lower()
if acc.lower() == 'quit':
break
else:
print("This is not an option.")
time.sleep(1)
Мне нужна помощь:
for li in data:
if(li.split(":")[1].lower() == del2):
print("Are you sure you want to delete this account?")
delok = input("You may lose all your data (for a long time)! Y/N\n>")
**
if delok.lower() == 'y':
file = open("E:/New folder/NO/How about no/accounts.txt", "r")
lines = f.readlines()
f.close()
file = open("E:/New folder/NO/How about no/accounts.txt", "w")
for line in lines:
if line !=
print("Deleted the account.")
sys.exit()
**
Удаление определенной строки в файле (python)
Это веб-сайт, на котором я работал, но онсделал бы только прозвище, которое я понял.Текстовый файл ниже:
Name:Password
aswitneto:llllllll
madda:mmmmmmmm
Я хочу удалить как никнейм, так и пароль .
Есть идеи?