using marshal module
Gordon McMillan
gmcm at hypernet.com
Tue Feb 1 23:27:40 EST 2000
Christine Davis writes:
>
> I am new to python (but old(er) to C++); and trying to get a script to
> work. What it is meant to do, is reads in a file, config.db and then
> changes the values of one of the fields, and write the revised contents
> back. if I call dump(<array where file contents loaded to>, <name of file
> pointer>) no change seems to take place. (The field isn't changed, nor
> does the field get concatenated at the end.
>
> I have this feeling I may be missing out something important, but silly.
You got that right <wink>. By leaving out the mode, you are
opening the file for read access. Just like in C, you probably
want "r+b" and a seek(0), or two files.
> Here is the piece of broken code:
>
> # open the file
> fp = open(filename)
> datastuff = marshal.load(fp)
>
> # change the field
> datastuff['field'] = 'new string of stuff'
>
> # "write it back to the file"
> # also tried "marshal.dump(datastuff, fp)" which didn't work either
> marshal.dump(datastuff['field'], fp)
>
> # close the file
> fp.close()
>
>
> --
> http://www.python.org/mailman/listinfo/python-list
- Gordon
More information about the Python-list
mailing list