我的 python 代码没有正确写入或读取文件,不知道哪里出了问题?
My python code is not writing or reading files correctly, do not know what is wrong?
我在 python 和在 Linux 中使用它仍然是新手,但我不明白这里出了什么问题。我在系统的开头导入了文件。我没有收到任何错误,但我知道有时选项 2 实际上并未写入文件。由于某种原因,我也无法重复选项 1。帮助将不胜感激。我相信菜单部分工作得很好,但 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()
我引用的文件是:
Regular_emp.txt
1001,'name 1',48000.00
1002,'name 2',55000.00
1003,'name 3',60000.00
和
Hourly_emp.txt
2001, 'name 4', 15.00, 160
2002, 'name 5', 20.00, 125
2003, 'name 6', 35.00, 123
在选项 2 中,您需要迭代 fileobject.read()
。作为 salary
和 hourly
本身,如果只是文件对象。
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()
我推荐用这种方法处理文件
# Writing
with open("file.txt","w") as f:
f.write("test")
# Reading
with open("file.txt","r") as f:
contents = f.read()
因此文件会在 with
缩进结束时自动关闭。保持文件打开一小段时间。
您可能遇到与选项 1 相同的问题。
我在 python 和在 Linux 中使用它仍然是新手,但我不明白这里出了什么问题。我在系统的开头导入了文件。我没有收到任何错误,但我知道有时选项 2 实际上并未写入文件。由于某种原因,我也无法重复选项 1。帮助将不胜感激。我相信菜单部分工作得很好,但 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()
我引用的文件是: Regular_emp.txt
1001,'name 1',48000.00
1002,'name 2',55000.00
1003,'name 3',60000.00
和 Hourly_emp.txt
2001, 'name 4', 15.00, 160
2002, 'name 5', 20.00, 125
2003, 'name 6', 35.00, 123
在选项 2 中,您需要迭代 fileobject.read()
。作为 salary
和 hourly
本身,如果只是文件对象。
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()
我推荐用这种方法处理文件
# Writing
with open("file.txt","w") as f:
f.write("test")
# Reading
with open("file.txt","r") as f:
contents = f.read()
因此文件会在 with
缩进结束时自动关闭。保持文件打开一小段时间。
您可能遇到与选项 1 相同的问题。