[Tutor] Newbie - Not reading file

Remco Gerlich scarblac@pino.selwerd.nl
Thu, 25 Oct 2001 18:53:33 +0200


On  0, Barbara Mark <bl_mark@hotmail.com> wrote:
> Hello,
> I am a programming newbie and Python newbie.  I need some very basic help 
> with my first program.  I've looked through some of the archives, and 
> someone has borrowed my copy of "Learning Python."
> The program does not seem to be reading in the file very well, although the 
> imported function calculation works fine.  It will do a correct calculation 
> if I offer it a number, but it will not grab the numbers from the opened 
> file.  I've tried many things, so what I have below is just a sample.  
> Please help, tell me what I'm doing wrong.
> 
> The program is:   TaxProgram
> The function is:  Tax8.py
> The file is:      Incomes.dat
> 
> *******I have tried many, but a version of the program reads:
> import sys
> import Tax8
> 
> t = Tax8.tax
> 
> x = open('incomes.dat', 'r+')
> 
> while x > 0:
> 	t(x)

What happens here is that the file is opened, and the open file is called x.

Nothing is actually read yet.

You can read a line of the file with x.readline(), then you have to do
something with the line. Also, it will be a string first, not a number, so
you have to change it into an int.

This should work:

import Tax8  # sys instead used

t = Tax8.tax  # This isn't actually necessary, but does no harm

x = open('incomes.dat', 'r') # For later, try giving your files better names :-)

while 1:
    # We're going to repeat this until the number is 0
    line = x.readline()
    number = int(line)
    if number == 0:
       # End of file, leave the while loop
       break
       
    t(number)
    
> *******Again, I have tried many, but a version of the function reads:
> def tax(x):      #calculates tax for input income
> 
> 	print x
> 	r = []
> 
> 	if 0 < x < 20000:
> 		r = x*0.1
> 
> 	elif 20000 < x < 60000:
> 		r = (20000 * 0.1) + ((x-20000) * 0.15)
> 
> 	elif 60000 < x < 200000:
> 		r = (20000 * 0.1) + (40000 * 0.15) + ((x-60000) * 0.25)
> 
> 	else:
> 		r = (20000 * 0.1) + (40000 * 0.15) + (200000 * 0.25) + ((x-200000) * 0.3)
> 
> 	print r
> 
> 	return r

One comment: usually a function either prints a result, or returns it so it
can be used elsewhere, but not both. In this case, the program above doesn't
do anything with the result and relies on the function to print it; it would
perhaps be better if you didn't print it here, but instead wrote 
'print t(number)' in the while loop above. Then you could use the same
function somewhere else, if you didn't want to print the results, but store
them or use them in some calculation.


 
> ******A sample incomes.dat file may read:
> 10000
> 15000
> 50000
> 0

And one comment here: this works, ending the file with 0. Until you actually
get a zero income in there!

Now .readline() on a file returns the empty string ('') when you call it at
the end of the file. So you could also end the loop like this:

while 1:
   line = x.readline()
   if line == '':
      break
   number = int(line)
   ... (etc)
   
And then you don't need to end your data file with a zero.

> ** Any help would be appreciated.
> 
> Thanks.  Blm

Hope this helps.

-- 
Remco Gerlich