On Oct 15, 12:47 pm, "erict1689" <[EMAIL PROTECTED]> wrote: > I am writing this program in which I open up a file and update that > information but to a new file. I already have a global variable for > it but how do I go about creating an openable file in the source code? > If it helps here is what I have: > > 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.
You can use 'open( "result.txt", "w" )' to create a file if it doesn't exist, and write to it. Use 'a' instead of 'w' to add to the end of an existing file. Later on, you might want to create an object and attributes instead of global variables. The readability is different, but if you're only using one such object, it's not necessarily better. You can always try it out and go back if you don't like it. class PayFile: def startUp( self ): self.payFile=open("payroll.txt", "r") self.payFile.readline() ... etc ... def closeUp( self ): self.payFile.close() self.payFileUpdated.close() print "\nNumber of records in the file was",self.recordCount payFile= PayFile( ) #create a new instance #or payFile= payfilemod.PayFile( ) in a separate module payFile.startUp( ) #call the startUp method Note the 'self' that precedes the variables. It acts as a container for them. It's worth changing in some cases, even if the code is merely more therapeutic to read. -- http://mail.python.org/mailman/listinfo/python-list