'My python code is not writing or reading files correctly, do not know what is wrong?
I am still pretty novice at python and using it in Linux but I do not understand what is going wrong here. I have the files imported in the beginning on my system. I am not getting any errors but I know that sometimes option 2 is not actually writing in the file. I also am unable to repeat option 1 for some reason. Help would be very greatly appreciated. The menu section works just fine I believe but there is just some error within MonthlySalary and/or Display_salary.
import os
salary=open('Regular_emp.txt','r')
hourly=open('Hourly_emp.txt','r')
def readEmployee(salary,hourly):
for line in salary:
line=line.strip()
number,name,salary=line.split(',')
print(number, "," , name)
for line in hourly:
line=line.strip()
number,name,wage,hours=line.split(',')
print(number, "," , name)
print("")
def monthlySalary(salary,hourly):
paycheck=open('Paychecks.txt','w')
for line in salary:
line=line.strip()
number,name,salary=line.split(',')
monthly=int(salary)/12
paycheck.write(str(number)+','+str(name)+','+str(format(monthly,'.2f')+"\n"))
for line in hourly:
line=line.strip()
number,name,wage,hours=line.split(',')
monthly=int(wage)*int(hours)
paycheck.write(str(number) + "," + str(name) + ','+str(format(monthly,'.2f'))+"\n")
paycheck.close()
def display_paycheck():
open('Paychecks.txt','r')
checkFile=os.path.getsize("Paychecks.txt")
while True:
if checkFile== 0:
print("")
print("Please enter option 2 first to create paycheck file")
print("")
break
else:
for line in paycheck:
line=line.strip()
number,name,paycheck=line.split(',')
print(number, "," , name,",",paycheck)
print("")
def menu():
print("Your options include:")
print("1. Display all employee details from both input files")
print("2. Calculate Monthly payment of all individuals")
print("3. Display paycheck details of all employees")
print("4. Exit the Program")
print("")
answer=input("Which option would you like?(Enter 1,2,3 or 4 to exit)")
while True:
if answer == "1":
readEmployee(salary,hourly)
elif answer=="2":
paychecks=monthlySalary(salary,hourly)
elif answer=="3":
display_paycheck()
elif answer == "exit" or answer == "4":
confirmation=input("Are you sure you would like to exit?(enter yes or no)")
if confirmation == "yes":
break
else:
print("Error has Occured please try again.")
print("Your options include:")
print("1. Display all employee details from both input files")
print("2. Calculate Monthly payment of all individuals")
print("3. Display paycheck details of all employees")
print("4. Exit the Program")
print("")
answer=input("Which option would you like?(Enter 1,2,3 or 4 to exit)")
menu()
The files I am referencing are: Regular_emp.txt
1001, 'name 1', 48000.00
1002, 'name 2', 55000.00
1003, 'name 3', 60000.00
and Hourly_emp.txt
2001, 'name 4', 15.00, 160
2002, 'name 5', 20.00, 125
2003, 'name 6', 35.00, 123
Solution 1:[1]
In option 2 you need to iterate over fileobject.read(). As salary and hourly themselves if just file objects.
def monthlySalary(salary, hourly):
paycheck = open('Paychecks.txt', 'w')
# Here
for line in salary.read():
line = line.strip()
number, name, salary = line.split(',')
monthly = int(salary)/12
paycheck.write(str(number)+','+str(name)+',' +
str(format(monthly, '.2f')+"\n"))
# And here
for line in hourly.read():
line = line.strip()
number, name, wage, hours = line.split(',')
monthly = int(wage)*int(hours)
paycheck.write(str(number) + "," + str(name) +
','+str(format(monthly, '.2f'))+"\n")
paycheck.close()
I recommend handing files in this method
# Writing
with open("file.txt","w") as f:
f.write("test")
# Reading
with open("file.txt","r") as f:
contents = f.read()
So the file is automatically closed at the end of the with indentation. Keeping the file opened for a short amount of time.
You are probably having the same issue with option 1.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
