Python 如何为我的文件指定由4个参数组成的名称?

Python 如何为我的文件指定由4个参数组成的名称?,python,numpy,Python,Numpy,我已经在本出版物中询问过如何将“我的迭代次数”作为文件名,我有一个很好的答案,但现在我需要给我的文件命名,该名称由4个参数组成,因为这将帮助我进行简单的解释: 这是我的代码: for b in Key_file: key_16b=b[0:32] key128=binascii.unhexlify(key_16b) Ciphertxt_file = open("C:\\Users\\user\\Win_My_Scripts\\Ciphertexts.txt", "w") Plain

我已经在本出版物中询问过如何将“我的迭代次数”作为文件名,我有一个很好的答案,但现在我需要给我的文件命名,该名称由4个参数组成,因为这将帮助我进行简单的解释: 这是我的代码:

for b in Key_file:
    key_16b=b[0:32]
    key128=binascii.unhexlify(key_16b)
Ciphertxt_file = open("C:\\Users\\user\\Win_My_Scripts\\Ciphertexts.txt", "w")
Plaintxt_file = open(r'C:\\Users\\user\\Win_My_Scripts\\Plaintexts.txt', 'r')
for line, a in enumerate(Plaintxt_file):
    plaintxt_16b=a[0:32]
    plaintext=binascii.unhexlify(plaintxt_16b)
    clear_msg=b'\x24'+b'\x73'+plaintext+b'\x74'+key128
    print('Trace number:', line)
    ser.write(clear_msg)
    time.sleep(0.5)
    ciphertext= (ser.read(250))
    print(ciphertext)
    Ciphertxt_file.write(ciphertext)
    dataArray = get_data(['C2'],8000,scope)
    fileName =  r'C:\\Users\\user\\My_resul\\Win_My_Scripts\\file_{}.npy'.format(line) 
    np.save(fileName, dataArray)
ser.close()                # close ports
我需要按如下方式命名每个文件:

AES_iteration=number_of_iteration_key128=key128_Plaintext=Plaintext1_Ciphertext=ciphertext1
例如:

AES_iteration=1_key128=0c0d0e0f08090a0b0405060700010203_Plaintext=7a8e5dc390781eab8df2c090bf4bebca_Ciphertext=17CFCD13 784AECB0 204375E1 C58ECBC3
通过这种方式,我将确保我的所有文件都是同步的,并且我得到了最好的结果


如果你能帮助我,我将非常感激

您只需以另一种方式格式化文件名:

fileName = (r'C:\\Users\\user\\My_resul\\Win_My_Scripts\\'
               'AES_iteration={}_key128={}_Plaintext={}_Ciphertext={}.npy'
               .format(iteration,key128,plaintext,ciphertext))
其中,
迭代
键128
明文
密文
是包含参数的变量(您应该自己定义)


.format
在格式化字符串中的“格式化字符串”上调用,
{}
表示应填写的参数。您可以使用提供给
.format(..)
调用的参数来填写这些参数。

阅读有关“format”的文档。请参阅-第6.1.3TypeError节:file()参数1必须是不带空字节的编码字符串,而不是str,这会导致此错误。在这种情况下,您的一个参数不是字符串,或者它引入了转义字符。你应该检查一下。