-1

I have a list of numpy files in one folder, I try to modify the name of each file in my folder 3_Traces_npy from this format:

AES_Trace=1234_key=000102030405060708090a0b0c0d0e0f_Plaintext=f9f19b259648feb20d842480745de16f_Ciphertext=a3140be40735f9f1865aa6b1b32b5667.npy

to this format:

dataArray1234_pti_f9f19b259648feb20d842480745de16f_cypher_a3140be40735f9f1865aa6b1b32b5667.npy

This is may code, where I am blocked how to create the nw name:

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))
Guillaume
  • 123
  • 4
  • 13

1 Answers1

0

You could parse the file name string using 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

Called in your for loop:

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))
Community
  • 1
  • 1
brennan
  • 3,392
  • 24
  • 42