472,141 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,141 software developers and data experts.

File management

I am writing a program in which i open up a file and read the contents
then do some calculations and update the information but in a new
file. So my question is how do i go about declaring the new file so
that the program will know that the new information goes into the new
file and not the original? if it helps here is the code that i have
so far:
def startUp():
# Purpose: opens files and print report headings
global empName, previousYTD, payRate, hoursWorked, recordCount,
eof, payFile, \
payFileUpdated, newYTD, currentPay
payFile=open("payroll.txt", "r")
payFile.readline()
def readRecord():
# Purpose: reads a record
global empName, previousYTD, payRate, hoursWorked, recordCount,
eof, payFile, \
payFileUpdated, newYTD, currentPay

employeeRec = payFile.readline()
if employeeRec == "":
eof = True
else:
# parse file line for record fields and format/convert for
final output
empName = employeeRec[0:25].strip()
previousYTD = float(employeeRec[25:40])
payRate = float(employeeRec[40:55])
hoursWorked = float(employeeRec[55:70])
recordCount += 1
eof = False

def writeRecord():
# Purpose: writes the updated record to the output file
#Parameter
global empName, previousYTD, payRate, hoursWorked, recordCount,
eof, payFile, \
payFileUpdated, newYTD, currentPay

def processRecords():
# Purpose: loops through input file and processes each record
global empName, previousYTD, payRate, hoursWorked, recordCount,
eof, payFile, \
payFileUpdated, newYTD, currentPay

while not eof:
calculatePay()
printReportLine()
writeRecord()
readRecord()

def calculatePay():
# Purpose: calculates pay and updated YTD
# Return values: float - calculated pay, float - updated YTD amount
global empName, previousYTD, payRate, hoursWorked, recordCount,
eof, payFile, \
payFileUpdated, newYTD, currentPay

def printReportLine():
# Purpose: prints employee pay information
# Parameters passed: float - calculated pay, float - updated YTD
amount
global empName, previousYTD, payRate, hoursWorked, recordCount,
eof, payFile, \
payFileUpdated, newYTD, currentPay

def closeUp():
# Purpose: end of program housekeeping
global empName, previousYTD, payRate, hoursWorked, recordCount,
eof, payFile, \
payFileUpdated, newYTD, currentPay

payFile.close()
payFileUpdated.close()
print "\nNumber of records in the file was",recordCount

any and all help is appreciated.

Oct 15 '08 #1
1 1239
erict1689 schrieb:
def closeUp():
# Purpose: end of program housekeeping
global empName, previousYTD, payRate, hoursWorked, recordCount,
eof, payFile, \
payFileUpdated, newYTD, currentPay

payFile.close()
payFileUpdated.close()
print "\nNumber of records in the file was",recordCount

any and all help is appreciated.
don't use global, here ist no need for.

if you need a output file so open one and use it.
def writeFile(fileName, content):
hd = open(fileName, 'wb');
hd.write(content)
hd.close

Oct 16 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tom | last post: by
6 posts views Thread by Codemonkey | last post: by
4 posts views Thread by TempEcho | last post: by
4 posts views Thread by Daven Thrice | last post: by
8 posts views Thread by Ritesh | last post: by
reply views Thread by Carlos | last post: by
5 posts views Thread by yazwas | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.