top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

pickle.dump never works

0 votes
161 views

I have a method that opens a file, lock it, pickle.load the file into a dictionary.I then modify the status of a record, then pickle.dump the dictionary back to the file.
The problem is that the pickle.dump never works. The file never gets updated.

def updateStatus(self, fp, stn, status):          
          f = open(fp, rw+)           
          fcntl.flock(f.fileno(),fcntl.LOCK_EX | fcntl.LOCK_NB)
          tb = pickle.load(f)
          self.modifyDict(tb, stn, status)
          pickle.dump(tb, f) 
          fcntl.flock(f.fileno(),fcntl.LOCK_UN)          f.close()

What could be the problem here?What mode should I use to open the file to allow both pickle.load and pickle.dump?

posted Jun 25, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

-- self.modifyDict(tb, stn, status)

f.seek(0)

-- What could be the problem here?

pickle.load() moves the file position to the end of the (first) pickle. pickle.dump() writes the modified dict starting at the current position. You end up with two versions of the dict, but you'll only ever read the first.

The fix is to go back to the start of the file with f.seek().

-- What mode should I use to open the file to allow both pickle.load and pickle.dump?

Assuming that the file already exists when updateStatus() is invoked for the first time: "r+b".
I usually open the file twice, once for reading and then for writing, but I guess that would interfere with locking.

answer Jun 25, 2013 by anonymous
f.seek(0)  really does the trick.
Similar Questions
+1 vote

I've a huge csv file and I want to read stuff from it again and again. Is it useful to pickle it and keep and then unpickle it whenever I need to use that data? Is it faster that accessing that file simply by opening it again and again? Please explain?

+3 votes

Discovered today: fopen() opens '.' and '..' but fread() returns nothing without errors nor end-of-file, so this loop:

 $f = fopen('.', 'r');
 if( $f === FALSE ) die("fopen failed");
 $n = 0;
 while(($s = fread($f, 10)) !== FALSE)
 $n += strlen($s);
 fclose($f);
 echo "total bytes read: $nn";

never ends! The same happens with '..'. Tested on PHP 5.6.3 and 7.0.0 under Slackware 14.1 32-bits. Under Vista, instead, fopen() fails as expected.

Bug or specific feature under Linux?

...