Home » Blog » Read data from two file and calculate salary of employee

Read data from two file and calculate salary of employee

Problem:

An organization wants to compute monthly wages to be paid to an employee in an organization. The input data is provided in two different files. File1 contains permanent employee data about employees (i.e. Empid, name, hourly wages), and File2 contains working hours information of each employee in the current month (i.e., empid and hours). Individual elements of data are separated by commas. Design a python program that reads both the files, computes the monthly wages of each employee and store in another file. Take both file names as command line arguments and check the respected exceptions for the same.
File Format:
File1
1001, Vinay kumar, 40
1002, Rohit sen, 35
1003, Vinita sharma, 28
File2
1001, 250
1002, 0
1003, 125

 

empdata=[]
emphour=[]
empsal=[]

filenames=["evaluatesal","emp1.txt","emp2.txt"]
f3=open("empsal.txt","w")

if len(filenames)<2:
    print("Not Have Any Argument")
elif len(filenames)!=3:
    print("Insufficient Argument Please Provide 2 File Names")
else:
    permenantdata=filenames[1]
    tempdata=filenames[2]

    try:
        f1=open(permenantdata,"r")
        f2=open(tempdata,"r")        
        
        f1data=f1.read().split('\n')
        f2data=f2.read().split('\n')
        
        for x in range(len(f1data)):
            empdata.append(f1data[x].split(','))
            emphour.append(f2data[x].split(','))
            
        print("Emp no\tEmp name\tHour wage\tHour\tSalary")
        for x in range(len(f1data)):
            sal=float(empdata[x][2])*float(emphour[x][1])
            data=empdata[x][0]+" , "+empdata[x][1]+" ,\t"+empdata[x][2]+" ,   \t"+emphour[x][1]+" ,\t"+str(sal)+"\n"
            print(data)
            f3.write(data)
            
            
    except  (FileNotFoundError) as e:
        print("Exception Occur",e)
    finally:
        print("done")
        f1.close()
        f2.close()
        f3.close()

OutPut

Emp no	Emp name	Hour wage	Hour	Salary
1001 ,  Vinay kumar ,	 40 ,   	 250  ,	10000.0

1002 ,  Rohit sen ,	 35 ,   	 0  ,	0.0

1003 ,  Vinita sharma ,	 28  ,   	 125  ,	3500.0

done

 

Leave a Reply

Your email address will not be published.