Forums

File IO not persistent?

Hi, I am a new member starting to learn python and I am glad to have found pythonanywhere since I can access my code and practice python anywhere very easily.

There's one issue having to do with file IO that I have been wondering about. When I use python installed in my computer I never seem to come across this issue but when I run it in these servers I have noticed that the txt files I have been using to store persistent data, always revert to some point in the past time. I am not sure exactly at what point this happens (after console shutdown maybe?).

I have tried looking into the file several times as some of my scripts run, and it does seem to successfully change as it should, but as I mentioned before, during some point in the future it reverts back to what it used to be?

Is there some sort of configuration that I must undergo for files to be modifiable persistently?

I hope I managed to explain my issue well enough so you guys can understand what I am talking about. (PS: I am currently not using any of the services related to dropbox)

Thanks in advance!

-Eric

Eric: A few questions to clarify what is going on here.

  1. If your code isn't private post it here so we can see for sure what is happening to help you. If it's private you don't need to post it, but consider posting the few lines that deal with your file handling.
  2. When you say "At some point in the future" do you mean days later? Hours later? Minutes later? And also do you mean after you have closed and restarted the python interpreter in question...or are you leaving the interpreter running during the whole time between file change and "some point in the future"?

There may be more, but let's start with this if you could.

Hi thanks for your reply.

When I say "at some point in the future" I mean seconds. In short, let me give you an example.

For example I have one script that is connected through a socket and it responds to events received from that socket (the program runs until it receives a specific event from the socket). -data may change through some of the events -script periodically saves data every minute or so -It seems that when i set off that "end/exit" event, and the whole script reaches its last sys.exit() the file reverts within

The module for loading and saving the data is pretty simple:

#loads data and returns:
#   a dictionary of people
#       with each person being a dict. with 3 components: name(string), numbersSet1(number list), numbersSet2(number list)
#           with each numberset being a list of 8 numbers Ex: numbersSet1 is [1,2,3,4,5,6,7,8]
def loadData(filename):
    i = 0;
    people = {}

    f = open(filename,'r')

    for line in f.readlines():
        if(i%3 == 0):       #name
            name = line.strip()
        elif(i%3 == 1):     #first set of numbers
            parts = line.partition('-')
            numbersSet1 = map(int, parts[2].split(':') )
        else:               #second set of numbers
            parts = line.partition('-')
            numbersSet2 = map(int, parts[2].split(':') )

            #now we consolidate elements into a "person structure"
            people[name] = {'name':name, 'numbersSet1':numbersSet1, 'numbersSet2':numbersSet2}
        i = i+1

    f.close()
    print "Loaded Data..."
    return people

#saves into file with following format:
#Joe
#    numbersSet1-1:2:3:4:5:6:7:8
#    numbersSet2-1:2:3:4:5:6:7:8
#Charles
#    numbersSet1-1:2:3:4:5:6:7:8
#    numbersSet2-1:2:3:4:5:6:7:8
#Bob
#    numbersSet1-1:2:3:4:5:6:7:8
#    numbersSet2-1:2:3:4:5:6:7:8
# ...and so on...
def saveData(filename,people):
    f = open(filename,'w')
    for key in people.keys():
        person = people[key]
        f.write(person['name'])
        f.write('\n\tnumbersSet1-')
        numbersSet1 = person['numbersSet1']
        f.write(str(numbersSet1[0]))
        for i in range(1,8):
            f.write(':')
            f.write(str(numbersSet1[i]))
        f.write('\n\tnumbersSet2-')
        numbersSet2 = person['numbersSet2']
        f.write(str(numbersSet2[0]))
        for i in range(1,8):
            f.write(':')
            f.write(str(numbersSet2[i]))
        f.write('\n')
    f.close()
    return

Where you would make calls such as

p = loadData("data.txt")

and

saveData("data.txt",p)

(Note: loadData only called once at the beginning of script) (Also, no exceptions or errors have ever been raised)

I have just confirmed it is upon console exit or killing the console.

TEST 1 <ol>

<li>I tested this out by putting a raw input line at the very end of the script to stall the console(after all saves have been done)</li>

<li>This means, ALL the commands have been executed except for this last raw input.</li>

<li>opened the data file and copied its contents into a notepad, data seemed fine.</li>

<li>then killed the console</li>

<li>I re-opened the data file and compared its contents with the previous version.</li>

<li>A couple of the new entries added during the script were gone.</li>

</ol> What is going on?

Well, I can't say for sure what's going on, but I have two suggestions:

1 - use the with syntax for file i/o - it helps to make sure that files are closed down neatly, and saves you from having to worry about file.close, and putting in try/finally code.

with open(filename, 'w') as f:
    f.write("all sorts of text")

cf the end of this section of the docs: http://docs.python.org/tutorial/inputoutput.html#methods-of-file-objects

2 - could it be that your sys.exit calls are happening before the file has finished writing? Your own PC and our OS might have slightly different behaviour in terms of how file I/O is buffered, and when things are actually written to disk. Using the with method above should help. You could also try using calls to f.flush() to see if that changes how the scripts behave...