[Tutor] dictionary looping problem

Alan Gauld alan.gauld at btinternet.com
Wed Dec 3 01:09:40 CET 2008

"Jeremiah Jester" <jeremiah.jester at panasonic.aero> wrote

> I've created a function for each dictionary. However, when i print 
> out
> the dictionary I don't get all the items. Any ideas?

You should only get a very small number indeed!

>        for filename in names:
>                hash=os.system("md5 "+ filename)
>                dict[hash] = filename

os.system only returns the exit code of the command not
the output of the command. In most cases that will be 0 to
indicate no errors.

You need to use the subprocess module (or any of the older
alternatives such as popen) to fetch the actual output of the
command from stdout and assign it to hash.

But better still use one of the python modules to do the
hashing instead of calling the OS!

I think the hashlib module's md5 function does what you want.

Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list