469,945 Members | 1,815 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

Problem with sha.new

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
Jul 21 '05 #1
4 1793
Florian Lindner wrote:
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.

--
Andrew Clover
mailto:an*@doxdesk.com
http://www.doxdesk.com/

Jul 21 '05 #2
Florian Lindner wrote:
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.
Jul 21 '05 #3
On Sat, 09 Jul 2005 13:49:12 +0200,
Florian Lindner <Fl*************@xgm.de> wrote:
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

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Jul 21 '05 #4
Florian Lindner wrote in news:da*************@news.t-online.com 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Kostatus | last post: by
117 posts views Thread by Peter Olcott | last post: by
18 posts views Thread by Ian Stanley | last post: by
28 posts views Thread by Jon Davis | last post: by
6 posts views Thread by Ammar | last post: by
2 posts views Thread by Mike Collins | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.