I have difficulties on deleting an account in a text file, although it is a school work.
Here is my code (long and complicated):
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)
The part I need help is:
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()
**
Deleting a specific line in a file (python)
This is the website I went on but it would only do the nickname I realised. The text file is below:
Name:Password aswitneto:llllllll madda:mmmmmmmm
I want to remove both nickname and password.
Any ideas?
Okay, it's still horribly flawed (what if there is a ":" in the username or password?), but at least the code is readable:
What you should learn from above code is not so much how I solved your problem, but how to write clean code. Use proper variable naming and write small reusable functions.