[Tutor] saveing and loading text data

Alan Gauld alan.gauld at btinternet.com
Wed May 13 19:10:43 CEST 2009


"Jacob Mansfield" <cyberjacob at googlemail.com> wrote

> hi everyone, I'm a bit new here but i was wondering if someone could 
> check
> some of my code, it's not doing quite what it's meant to.

That's not very specific. What is it meant to do?
What does it actually do? Do you get any error messages?
If so post them in the email.

> the problem is when you start the application again and
> enter the databox.txt to load from thanks

With no idea of what databox.txt is that doesn't help too much either.

> Databox_2_0.py:
>
> import sys, os
> pygame.init()
> def load(filename):
>    if filename != '':
>        e = 1
>        dec = "placeholder"
>        fic = open(filename, "r")
>        while dec != '':

This would be a lot neater using a for loop.

>            num = str(e)
>            print "found " + num + " enteries"
>            dec = fic.readline(e)

This is almost certainly wrong.
an argument to readline will read (up to) that many characters.
I have no idea what you thought it would do... So in this case
dec will contain 1 character the first time through, 2 characters
the second time etc.

>            databox[e] = dec
>            dec = fic.readline((e+1))

And it will contain 2 then 3 characters here...

>            databox2[e] = dec
>            e = e+1
>        fic.close()
>    else:
>        return 0

Returning 0 is probably not that useful? The funcvtion will return None
the rest of the time. Both evaluate to False in a boolean test so code
like this

if not load():
   print "Load failed"

will always fail. If you returned a non zero value you could at least
compare success and failure more easily. However it would be
better still to just allow the eception to flow through IMHO!

> def search():
>    print "\n"
>    x = 1
>    items = len(databox)
>    ins = items+1
>    while ins > x :
>        dac = databox[x]
>        dac2 = databox2[x]
>        x = x + 1
>        print dac + " " + dac2
>    print "\n\n"

Not sure what this is intended to do but it doesn't look like
a search!

> def add():
>    dat = raw_input("First name.\n")
>    dat2 = raw_input("\nSecond name.\n")
>    items = len(databox)
>    ins = items+1
>    databox[ins] = dat
>    databox2[ins] = dat2
>    print "Done.\n\n"
> def exitprog():
>    fic = open('databox.txt','w')
>    print "saveing\n"
>    x = 1
>    items = len(databox)
>    ins = items+1
>    while ins > x :
>        dac = databox[x]
>        dac2 = databox2[x]
>        x = x + 1
>        fic.write(dac)
>        fic.write(dac2)

Note that you are not writing newline characters here
so the data will all be on a single line.


>    fic.close()
>    print "goodbye"
>    pygame.time.delay(900)
>    exit()

Should this be sys.exit()??

> databox = dict()
> databox2 = dict()
> go = raw_input("filename, blank for none.\n")
> load(go)

The return value from load() is irrelevant since you don't
check it!

> while True:
>    print "Welcome to databox V2.0."
>    print "     1. Searth the database."
>    print "     2. Add a record."
>    print "     3. Exit."
>    inme = raw_input("Please make a selection.\n")
>    if inme == "1":
>        search()
>    elif inme == "2":
>        add()
>    elif inme == "3":
>        exitprog()
>    else:
>        print "input not recignised."
>


There are a lot of things there that could be making it
"not quite right". You need to give us more information
if these ideas don't fix it.

Alan G.




More information about the Tutor mailing list