Hello I am having trouble modifying lines of text using python. I am making a payroll app and I cannot get the modify method to work. I do not understand why the entry that I search for will not delete in the new files. I am copying the files over to a temp and then renaming the temp file but the old entry stays in.
def modEmp():
empFile = open("employees.txt", 'r')
empFile2= open("temp.txt",'w')
line1 = empFile.readline().rstrip("\n")
name = input("Enter in your employee name that you would like to modify\n")
while line1 != '':
line1 = line1.split(" ") #split line into list
name = line1[0] + ' ' + line1[1]
if name[0] == line1[0] and name[0] == line1[0]:
print('Enter the modified entry:\n')
list = [0] * 4
list [0] = input('Enter first name:\n')
list [1] = input('Enter last name:\n')
list [2] = input('Enter pay rate:\n')
list [3] = input('Enter hours worked:\n')
empFile2.write(list[0] + ' ' + list[1] + ' ' + list[2] + ' ' + list[3] + "\n")
else:
empFile2.write(line1 + "\n")
line1 = empFile.readline().rstrip("\n")
#Close file
empFile.close()
empFile2.close()
os.remove('employees.txt')
os.rename('temp.txt','employees.txt')
if name[0] == line1[0] and name[0] == line1[0]:This doesn't make sense!!??nameis a string,line1is a list of string, thereforename[0] == line1[0]compares a character to a string, ie only works for people with 1-letter names ;-)