views:

653

answers:

5

I'm learning Python-this gives me an IO error-

f = open('money.txt')
while True:
    currentmoney = float(f.readline())
    print(currentmoney, end='')
    if currentmoney >= 0:
        howmuch = (float(input('How much did you put in or take out?:')))

        now = currentmoney + howmuch
        print(now)
        str(now)
        f.close()
    f = open('money.txt', 'w')
    f.write(str(now))
    f.close()

Thanks!

A: 

You close your file only if the IF condition is satisfied, otherwise you attempt to reopen it after the IF block. Depending on the result you want to achieve you will want to either remove the f.close call, or add an ELSE branch and remove the second f.open call. Anyway let me warn you that the str(now) in your IF block is just deprecated as you're not saving the result of that call anywhere.

emaster70
+2  A: 

well theres a couple of things...

you open(money.txt) outside the while loop but you close it after the first iteration... (technically you close, reopen & close again)

Put when the loop comes round the second time, f will be closed and f.readLine() will most likely fail

Eoin Campbell
+3  A: 

The while True is going to loop forever unless you break it with break.

The I/O error is probably because when you have run through the loop once the last thing you do is f.close(), which closes the file. When execution continues with the loop in the line currentmoney = float(f.readline()): f will be a closed filehandle that you can't read from.

monowerker
A: 

You'll get an IO Error on your first line if money.txt doesn't exist.

Sliff
A: 

Can I piggyback a question? The following has puzzled me for some time. I always get an IOError from these 'open()' statements, so I've stopped checking for the error. (Don't like to do that!) What's wrong with my code? The 'if IOError:' test shown in comments was originally right after the statement with 'open()'.

if __name__ == '__main__':
#get name of input file and open() infobj
    infname = sys.argv[1]
    print 'infname is:  %s' % (sys.argv[1])
    infobj = open( infname, 'rU' )
    print 'infobj is:  %s' % infobj
# 'if IOError:' always evals to True!?!
#   if IOError:
#       print 'IOError opening file tmp with mode rU.'
#       sys.exit( 1)

#get name of output file and open() outfobj
    outfname = sys.argv[2]
    print 'outfname is: %s' % (sys.argv[2])
    outfobj = open( outfname, 'w' )
    print 'outfobj is: %s' % outfobj
#   if IOError:
#       print 'IOError opening file otmp with mode w.'
#       sys.exit( 2)
behindthefall
IOError is an exception. You don't check for exceptions with 'if', you do it with try.... except IOError. IOError itself, like any value except for 0, False and an empty list, evaluates to True.
Daniel Roseman
You know, I said to myself as I was typing that, "I wonder if I've ever tried 'try ... except ...' there. THANK YOU for a clear, precise, full, and accurate answer. Not to mention nearly instantaneous! This is a very nice site.
behindthefall
Please do not "piggyback" questions. Ask a separate, easy to search for, discrete question so it can have an easy-to-search-for discrete set of answers. Feel free to delete this non-answer and ask a proper question.
S.Lott