I have modified your code. You should check if your dict contains the required key or not (I have added comments to the related parts in the code. Please see below).
Code:
def allicate(a_dict):
ans_dict = {}
for key, item in a_dict.items():
if item in ans_dict: # If key exists in your dict.
ans_dict[item].append(key) # Append the element to the list.
continue # Get next element.
ans_dict[item] = [key] # Create a new key if it doesn't exist in you dict.
return ans_dict
my_dict = {"name.txt": "Ram", "teach.txt": "Shyam", "cod.txt": "Ram"}
print(allicate(my_dict))
Output:
>>> python3 test.py
{'Ram': ['name.txt', 'cod.txt'], 'Shyam': ['teach.txt']}