33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
import os
|
|
import hashlib
|
|
import sys
|
|
|
|
# Pfad zum Verzeichnis, das überprüft werden soll
|
|
if (len(sys.argv)) != 2:
|
|
exit(-1)
|
|
|
|
directory_path = sys.argv[1]
|
|
|
|
# Dictionary zum Speichern der Dateiinhalte und ihrer Namen
|
|
file_contents = {}
|
|
|
|
# Durchlaufen aller Dateien im Verzeichnis
|
|
for filename in os.listdir(directory_path):
|
|
file_path = os.path.join(directory_path, filename)
|
|
|
|
# Überprüfen, ob es sich um eine Datei handelt
|
|
if os.path.isfile(file_path):
|
|
# Hash-Wert des Dateiinhalts berechnen
|
|
with open(file_path, 'rb') as file:
|
|
content_hash = hashlib.md5(file.read()).hexdigest()
|
|
|
|
# Überprüfen, ob der Dateiinhalt bereits vorhanden ist
|
|
if content_hash in file_contents:
|
|
# Datei umbenennen, wenn sie doppelt ist
|
|
new_filename = f"deleted_{filename}"
|
|
os.rename(file_path, os.path.join(directory_path, new_filename))
|
|
print(f"Datei {filename} umbenannt in {new_filename}")
|
|
else:
|
|
# Dateiinhalt im Dictionary speichern
|
|
file_contents[content_hash] = filename
|