0

I wrote this code that calculate the hash value for a pdf file and add it to a dictionary and save it to a file like this:

v={hash_value:{"file name":file_name,"number":1}}

But the problem with the below code is that if I add a new file it overwrite the previous one.

f = calculate_hash("calc.pdf")
v = {f:{"file name":"calc.pdf","number":1}}

with open('filename.pickle', 'wb') as handle:
    pickle.dump(v, handle)  

with open('filename.pickle', 'rb') as handle:
    b = pickle.load(handle)

x=  calculate_hash("calc.pdf")
for key in b:
    print key
    if x == key:
        print "yes"
3
  • 1
    Yes, open('filename.pickle', 'wb') clears the file - is the case that you wanted first to load the distionary(s), amend them, and then store again? Commented Jul 30, 2014 at 13:35
  • yes that is what I want Commented Jul 30, 2014 at 13:46
  • 1
    The why don't you read the file first and write it back after the modifications? Commented Jul 30, 2014 at 13:50

3 Answers 3

1

Just use 'append' mode:

with open('filename.pickle', 'wb') as handle:

=>

with open('filename.pickle', 'ab') as handle:
Sign up to request clarification or add additional context in comments.

1 Comment

Although generally a correct way to add to the end of a file, this is more complex with pickle. You will then need to deal with the multiple objects separately on load: see e.g. stackoverflow.com/a/15463472/3001761
0

when you open a file, you need to use the 'a' for append, instead of 'w'.

see reading-and-writing-files

1 Comment

When adding a link to your answer you should include an extract of the relevant information provided by the link.
0

The main error here is that you write to the file before you read it. Thereby overwriting all existing values rather than combining the existing values with the new one.

How about this?

import pickle
import random

def calculate_hash(s):
    return random.randint(1, 100) # not proper hash

f = calculate_hash("calc.pdf")
v = {f:{"file name":"calc.pdf","number":1}}

# read before you write
with open('filename.pickle', 'rb') as handle: # file has to exist here
    b = pickle.load(handle)

# combine the two dictionaries
b = dict(b.items() + v.items())

# write the combined dictionaries
with open('filename.pickle', 'wb') as handle:
    pickle.dump(b, handle)  

x=  calculate_hash("calc.pdf")
for key in b:
    print key
    if x == key:
        print "yes"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.