我在一个文件夹中有一个numpy文件列表,我尝试从这种格式修改我的文件夹3_Traces_npy
中每个文件的名称:
AES_Trace=1234_key=000102030405060708090a0b0c0d0e0f_Plaintext=f9f19b259648feb20d842480745de16f_Ciphertext=a3140be40735f9f1865aa6b1b32b5667.npy
采用以下格式:
dataArray1234_pti_f9f19b259648feb20d842480745de16f_cypher_a3140be40735f9f1865aa6b1b32b5667.npy
这可能是代码,我被阻止了如何创建nw名称:
import os
path_For_Numpy_Files='C:\\Users\\user\\3_Traces_npy'
os.chdir(path_For_Numpy_Files)
list_files_Without_Sort=os.listdir(os.getcwd())
list_files_Sorted=sorted((list_files_Without_Sort),key=os.path.getmtime)
for file in list_files_Sorted:
new_name=
os.rename(os.path.join(path_For_Numpy_Files, file), os.path.join(path_For_Numpy_Files, new_name))
答案 0 :(得分:0)
您可以使用split解析文件名字符串:
def reformat_name(filename):
key1 = filename.split('_Plaintext=')[1].split('_Ciphertext=')[0]
key2 = filename.split('_Ciphertext=')[1].split('.npy')[0]
title = filename.split('AES_Trace=')[1].split('_key=')[0]
new_filename = 'dataArray' + title + '_pti_' + key1 + '_cypher_' + key2 + '.npy'
return new_filename
在你的for循环中调用:
for file in list_files_Sorted:
new_name = reformat_name(file)
os.rename(os.path.join(path_For_Numpy_Files, file), os.path.join(path_For_Numpy_Files, new_name))