Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python for in-for循环:FileExistError_Python_Encryption - Fatal编程技术网

Python for in-for循环:FileExistError

Python for in-for循环:FileExistError,python,encryption,Python,Encryption,我正在编写一个程序,对文件进行加密并用密钥解密。代码: 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

我正在编写一个程序,对文件进行加密并用密钥解密。代码:

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 = 'abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&'
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()
我遇到的问题是,第二个文件通过解密重命名为第一个文件。这将导致FileExistError。以下内容可能有问题:

x += 1 
def Decrypt()中

如何解决这个问题

编辑:完全回溯: 回溯(最近一次呼叫最后一次): 文件“crypto.py”,第29行,在Decrypt中

os.rename(bestand_naam, locatie)

FileExistError:[WinError 183]无法生成已存在的文件。

代码中存在多个问题

def Decrypt():
    x = 0
    decryptie = input('Enter your decryption code: \n')
    if (decryptie == sleutel):
        for bestand in bestand_list:
            os.rename(bestandnaam_list[x], bestand)
            x += 1
        print ('Your file will now be decrypted')
    else:
        print ('Unfortunately your file was not decrypted')
这应该能奏效

顺便说一句,当您提交代码时,请务必打印您正在为您和我们做的事情,并使用英文varnames,因为我不知道sleutel和其他变量名称的含义

你做了2个forloops,给os.rename提供了错误的多个文件名/路径

忘了加上这个

def Encrypt
中直接调用Decrypt,将其移动到文件末尾

像这样:

Encrypt()

解密()。还有其他有用的信息,例如,哪一行出现问题。将立即编辑重命名的目的/目的是什么?您希望它如何工作?输入密钥后,文件名将返回原始文件。问题是解密时第二个文件与第一个文件的名称相同。这可能是因为您正在对函数中的变量进行所有更改。你真的确定你改变了最佳状态和最佳状态吗?试着打印一些图片,看看这是否真的在你的功能之外发生了变化,这主要是因为在可变内容方面,它本身就是一个有限的空间。换句话说,您希望变量像全局变量一样工作,但您没有确保它们是全局的…,顺便说一句,应该避免(有人说)…感谢您的解决方案!x可以用于bestand_列表和bestandnaam_列表!我再次编辑了代码以使其更短,在Decrypt()中不再声明/需要locatie