tags:

views:

131

answers:

2

I am new to programing and am having difficulties writing a program dealing with files. The program is to read a file, calculate an employees pay and an updated YTD pay total. After calculations the program will write to a new file.

This is what I have so far:

empName = ""
prevYTD = 0.0
payRate = 0.0
hoursWorked = 0.0
recordCount = 0

def startUp():
    global empFile
    print "\n\n" + "PAYROLL REPORT".center(110)+"\n"
    print "Employee Name".ljust(30) + "Previous YTD".ljust(18) + \
          "Updated YTD".ljust(18) + "Pay Rate".ljust(13) + \
          "Hours Worked".ljust(19) + "Current Pay".ljust(8)
    print "-"* 109

    jobInfo = open("payroll_Assignment#7.txt", "r")

def readFile():
    global empName, prevYTD, payRate, hoursWorked, eof
    empRec = jobInfo.readline()
    if empRec == "":
        eof = True
    else:
        empName = empRec[:25]
        prevYTD = float(empRec[25:40])
        payRate = float(empRec[40:55])
        hoursWorked = float(empRec[55:])
        eof = False

def processRecords():
    global recordCount
    while not eof:
        recordCount +=1
        printRecord()
        readFile()


def printRecord():
    print empName, prevYTD, payRate, hoursWorked


def closeUp(): 
    jobInfo.close()
    print "\nNumber of records in the file was",recordCount

startUp()
readFile()
processRecords()
printRecord()
closeUp() 

My problem is making a new file. The program is suppose to write to a new file and I don't know how to do it. Sorry for being so clumsy with this, I'm very new to it.

+1  A: 

Not sure what the problem is but some idiom can make it easy for you.

  1. You can avoid testing for EOF and the while loop.

File is iteratable hence you can iterate over it.

for line in open('myfile','r'):
    doSomething(line)

See the details at : http://docs.python.org/tutorial/inputoutput.html

[Edit: Based on revised problem]

Opening a new file for writing should be easy in python

>>> logfile = open('test.log', 'w') # Opens a new file
>>> logfile = open('test.log', 'a') # Opens a existing file to append information

Look at the various modes of opening file in Python tutorial

pyfunc
A: 

Thank you for your answers. The program is to read a file, calculate employee pay and update the YTD total. After the calculations the program is to write a new file. I can not figure out how to do the calculations, make, write, or save the updated file.

Greg
You can edit your question and add this text to it. Afterwards please delete this "answer" because obviously it is no answer to your question.
Felix Kling
@Greg: I have edited the question and my answer. You can delete your reply. Use comments, when you want to reply to others replies and posts.
pyfunc
Two Gregs? or Greg with two icons?
Tony Veijalainen
The program is to read a file, calculate an employees pay and an updated YTD pay total. After calculations the program will write to a new file. I can not get the program to do the calculations or make, write, or save the updated file.
Greg