StandardNerd StandardNerd - 7 months ago 28
Python Question

Code that runs in Python 2.7 breaks with Python 3.5.1

I have Python Code that runs fine with Python 2.7 but when i run it with Python 3 it doesn't work properly:

import sys, os, hashlib

dir_name = sys.argv[1]

# Get the total number of args passed to the crawler.py and set default
# directory
def check_arguments():
global dir_name
total = len(sys.argv)
if total > 2:
print("Bitte geben Sie nur ein Argument als Startverzeichnis an.")
sys.exit()
elif total == 2:
try:
os.listdir(dir_name)
print("Startverzeichnis " + dir_name + " wird verwendet.")
except:
print("Geben Sie ein g├╝ltiges Startverzeichnis an.")
sys.exit()
else:
print("Startverzeichnis /tmp wird verwendet.")
dir_name = '/tmp'

def create_MD5(dir_name):
file_list = []
hash_list = []
for file in os.listdir(dir_name):
try:
if os.path.isfile(os.path.join(dir_name, file)):
print("Folgende Datei gefunden: " , os.path.join(dir_name, file))
file_list.append(file)
hash = hashlib.md5.new()
f = open(os.path.join(dir_name, file),'rb')
hash.update(f.read())
f.close()
hash_list.append(hash.digest())

elif os.path.isdir(os.path.join(dir_name, file)):
print("Folgendes Verzeichnis gefunden:", os.path.join(dir_name, file))
#print_files()
fl, hl = create_MD5(os.path.join(dir_name, file))
for i in fl:
file_list.append(i)
for i in hl:
hash_list.append(i)
else:
pass
except:
print("Fehlende Zugriffsrechte")

return file_list, hash_list

def print_MD5():
fl, hl = create_MD5(dir_name)
for i in range(len(fl)):
print(os.path.join("Die Datei ", '%-22s hat folgenden MD5-Hash: %s' % (fl[i], hl[i])))

check_arguments()
print_MD5()


in the console i got:

Traceback (most recent call last):
File "crawler.py", line 77, in <module>
print_MD5()
File "crawler.py", line 74, in print_MD5
print(os.path.join("Die Datei ", '%-22s hat folgenden MD5-Hash: %s' % (fl[i], hl[i])))
IndexError: list index out of range


How can i change the code so that it runs with Python 3, too?

Answer

The line:

hashlib.md5.new()

is incorrect and should be:

hashlib.md5()

This is throwing an error which your exception eating code is eating. This is why a general except: that simply continues regardless is a bad idea, especially in a case such as yours where this potentially leaves the program in a bad state.

Why this works on 2.7 I do not know but I have suspicion it may not be doing what you think it is doing.

Also since hashlib in 3.x reseeds the hash when it it created you would be better off creating a single instance with known hash.

Comments