3

I have a file hash_db.pickle that when I created it I saved a dictionary on it:

v = {hash_value:{"file name":file_name,"file size":file_size,"last scanned time":scanned_time}}

{123dfre345:{"file name":calc.pdf,"file size":234,"last scanned time":12:23 24/12/2013}}
{3gcdshj754:{"file name":star.pdf,"file size":10,"last scanned time":10:30 10/10/2013}}

so if I want to change from the file only last scanned time for 3gcdshj754

how could I do that?

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
user3832061
  • 477
  • 3
  • 7
  • 12

2 Answers2

5

Using pickle is pretty simple, when writing, use

dct = {'3gcdshj754': {'file name': 'star.pdf', 'last scanned time': '10:30 10/10/2014', 'file size': '10'}}

import pickle
pickle.dump(dct, open("save.p", "wb"))

and then, when reading it, use

import pickle
dct_read = pickle.load(open("save.p", "rb"))

Note that either time, you have to open the file in binary mode (b flag).

Editing the content is now simple:

dct_read.values()[0]["last scanned time"] = '10:10 10/10/2010'

Alternatively, as @mhawke suggests in his answer, you can use shelve.

Community
  • 1
  • 1
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • it gives me an error "dct_read.values(0)['last scanned time'] = '10:10 10/10/2010' TypeError: values() takes no arguments (1 given)" – user3832061 Jul 31 '14 at 07:53
  • that is perfect thank you ... I want to ask another question if I can. what if I have many entries in the "save.p" file and I want to edit based on thefirst number like '3gcdshj754' which I considered it as a hash value – user3832061 Jul 31 '14 at 08:08
  • @user3832061 Then ask it as another question :) – Anshul Goyal Jul 31 '14 at 08:33
2

You can use pickle.

import pickle
d = pickle.load(open('hash_db.pickle', 'rb'))
d['3gcdshj754']['last scanned time'] = '11:30 11/10/2015'
pickle.dump(d, open('hash_db.pickle', 'wb'))

But you might find the shelve module a little more convenient than direct use of pickle. It provides a persistent dictionary which seems to be exactly what you want. Sample usage:

import shelve
from datetime import datetime, timedelta

# create a "shelf"
shelf = shelve.open('hash_db.shelve')
shelf['123dfre345'] = {"file name": 'calc.pdf', "file size": 234, "last scanned time": datetime(2013, 12, 24, 12, 23)}
shelf['3gcdshj754'] = {"file name": 'star.pdf', "file size": 10, "last scanned time": datetime(2013, 10, 10, 10, 30)}
shelf.close()

# open, update and close
shelf = shelve.open('hash_db.shelve')
file_info = shelf['3gcdshj754']
file_info['last scanned time'] += timedelta(hours=+1, minutes=12)
shelf['3gcdshj754'] = file_info
shelf.close()

And that's it.

mhawke
  • 84,695
  • 9
  • 117
  • 138
  • but what if I want to read and print all the 'hash_db.shelve' file ? – user3832061 Jul 31 '14 at 08:53
  • @user3832061 : shelf is like a dictionary so the normal dictionary functions apply, e.g. `shelf.keys()`, `shelf.items()`, etc. – mhawke Jul 31 '14 at 09:00
  • could I use this : shelf = shelve.open('hash_db.shelve') strings = re.findall(hash_value,shelf) – user3832061 Jul 31 '14 at 09:38
  • What happened when you tried it? You want to find those keys (hash_value) that match a regex? Something like this might work `[(k,v) for k,v in shelf.items() if re.match(pattern, k)]`. But note that this does not seem useful - there will not be any discernible pattern in a collection of hashes for which a regex will be useful. – mhawke Jul 31 '14 at 10:31
  • If you have the hash_value, just look it up directly. `shelf[hash_value]` will return the dictionary that contains the file information. – mhawke Jul 31 '14 at 10:33