CPickle.dump always dumps to the end of the file

cPickle.dump(object,file)

always unloaded at the end of the file. Is there a way to dump at a specific position in the file? I expected the following snippet to work

file = open("test","ab")
file.seek(50,0)
cPickle.dump(object, file)
file.close()

      

However, the above snippet dumps the object at the end of the file (assuming the file already contains 1000 characters), no matter where I look for the file pointer.

+3


source to share


1 answer


I think it might be more of a problem with the way you open the file than with cPickle

. ab

except that it's add mode (which shouldn't have anything to do with you seek

) provides a flag O_TRUNC

for the low-level syscall open

. If you don't want truncation, you should try mode r+

.

If that doesn't solve the yout problem and your objects are not very large, you can still use dumps

:



file = open("test","ab")
file.seek(50,0)
dumped= cPickle.dumps(object)
file.write(dumped)
file.close()

      

+2


source







All Articles