I need to convert a list of numpy files in one directory to a list of binary files in an other directory with keeping the same order and the same names: the only thing that I need to change it is the extension in the name of the file; For example:
My directory where I put my numpy files:
Tracenumber=01_Pltx1.npy
Tracenumber=02_Pltx2.npy
Tracenumber=03_Pltx3.npy
Tracenumber=04_Pltx4.npy
Tracenumber=05_Pltx5.npy
Tracenumber=06_Pltx6.npy
The resulted directory where I put my binary files:
Tracenumber=01_Pltx1.bin
Tracenumber=02_Pltx2.bin
Tracenumber=03_Pltx3.bin
Tracenumber=04_Pltx4.bin
Tracenumber=05_Pltx5.bin
Tracenumber=06_Pltx6.bin
At first, I use to convert this code to convert one numpy file to a binary file:
import numpy as np
import struct
traces = np.load('C:\\Users\\user\\My_Test_Traces\\1000_Traces_npy\\Tracenumber=01_Pltx1.npy')
f = open('C:\\Users\\user\\My_Test_Traces\\1000_Traces_bin\\Tracenumber=01_Pltx1.bin', 'wb')
for t in traces.flatten():
f.write(struct.pack('f', t))
f.close()
but now, I have tried this code:
import os
import numpy as np
import struct
path_For_Binary_Files='C:\\Users\\user\\My_Test_Traces\\1000_Traces_bin'
os.chdir('C:\\Users\\user\\My_Test_Traces\\1000_Traces_npy')
for root, dirs, files in os.walk(r'C:\\Users\\user\\My_Test_Traces\\1000_Traces_npy'):
for file in files:
f=open(file,'r')
trace= np.load(file)
for t in trace.flatten():
file.write(struct.pack(path_For_Binary_Files, t))
file.close()
But it doesn't give me the result that I want. I have this error.
file.write(struct.pack(path_For_Binary_Files, t))
AttributeError: 'str' object has no attribute 'write'