Problem with sha.new

  • Thread starter Florian Lindner
  • Start date
F

Florian Lindner

Hello,
I try to compute SHA hashes for different files:


for root, dirs, files in os.walk(sys.argv[1]):
for file in files:
path = os.path.join(root, file)
print path
f = open(path)
sha = sha.new(f.read())
sha.update(f.read())
print sha.hexdigest()


this generates a traceback when sha.new() is called for the second time:

/home/florian/testdir/testfile
c95ad0ce54f903e1568facb2b120ca9210f6778f
/home/florian/testdir/testfile2
Traceback (most recent call last):
File "duplicatefinder.py", line 11, in ?
sha = sha.new(f.read())
AttributeError: new

What is wrong there?

Thanks,

Florian
 
A

and-google

Florian said:
sha = sha.new(f.read())
this generates a traceback when sha.new() is called for the second time

You have reassigned the variable 'sha'.

First time around, sha is the sha module object as obtained by 'import
sha'. Second time around, sha is the SHA hashing object you used the
first time around. This does not have a 'new' method.

Python does not have separate namespaces for packages and variables.
Modules are stored in variables just like any other object.
 
J

John Machin

Florian said:
Hello,
I try to compute SHA hashes for different files:


for root, dirs, files in os.walk(sys.argv[1]):



for file in files:
path = os.path.join(root, file)
print path
f = open(path)
print "sha is", repr(sha) ### self-help !!!!!
sha = sha.new(f.read())
print "sha is", repr(sha) ### self-help !!!!!
sha.update(f.read())
print sha.hexdigest()


this generates a traceback when sha.new() is called for the second time:

/home/florian/testdir/testfile
c95ad0ce54f903e1568facb2b120ca9210f6778f
/home/florian/testdir/testfile2
Traceback (most recent call last):
File "duplicatefinder.py", line 11, in ?
sha = sha.new(f.read())
AttributeError: new

What is wrong there?
"sha" no longer refers to the module of that name; it refers to the
sha-object returned by the FIRST call of sha.new. That sha-object
doesn't have a method called "new", hence the AttributeError exception.
 
D

Dan Sommers

Hello,
I try to compute SHA hashes for different files:

for root, dirs, files in os.walk(sys.argv[1]):
for file in files:
path = os.path.join(root, file)
print path
f = open(path)
sha = sha.new(f.read())
^^^

Now the name "sha" no longer refers to the sha module, but to the object
returned by sha.new. Use a different name.
sha.update(f.read())
print sha.hexdigest()

Regards,
Dan
 
R

Rob Williscroft

Florian Lindner wrote in in
comp.lang.python:
Hello,
I try to compute SHA hashes for different files:


for root, dirs, files in os.walk(sys.argv[1]):
for file in files:
path = os.path.join(root, file)
print path
f = open(path)

Here you rebind 'sha' from what it was before (presumably the module sha)
to the result of 'sha.new' presumably the new 'sha' doesn't have a 'new'
method. try renameing your result variable.
sha = sha.new(f.read())
sha.update(f.read())
print sha.hexdigest()

result = sha.new(f.read())
result.update(f.read())
print result.hexdigest()

also I don't think you need the second call to update as f will
have been read by this time and it will add nothing.
this generates a traceback when sha.new() is called for the second
time:
sha = sha.new(f.read())
AttributeError: new

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top