Kris1511 Kris1511 -4 years ago 159
Python Question

Trying to find duplicate files within Python - Getting error global name not defined?

I am trying to find files, calculate their hash and hash to dictionary. Trying to find duplicate files based on their hash. I am not sure why I get this following error:

NameError: global name 'value_occurences' is not defined

import os
import hashlib
import collections

# Set the directory you want to start from
rootDir = 'C:\\Users\\kd\\prep'

def parsing_dir_files(rootDir):
for dirName, subdirList, fileList in os.walk(rootDir):
for fname in fileList:
hash_of_files = hashlib.md5()
hash_of_files = hash_of_files.hexdigest()

data_dict = {fname:hash_of_files}

return data_dict

def return_dups(file_list_dict):

value_occurrences = collections.Counter(file_list_dict.values())
filtered_dict = {key: value for key, value in file_list_dict.items()
if value_occurences[value] == 1}

print (filtered_dict)
return filtered_dict

def main():
file_list = parsing_dir_files(rootDir)
dup_list = return_dups(file_list)


Answer Source

value_occurrences not value_occurences,you forgot a r.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download