1

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'

  • you should describe what happens, what you expect to happen, how they differ and any errors you receive. – pvg Mar 21 '17 at 10:44
  • @pvg, could you take a look in the edited question please? –  Mar 21 '17 at 10:48
  • You shouldn't use \\ when your path is a raw string in the param to `os.walk`. Read the error message, that's obviously not a sane path. – pvg Mar 21 '17 at 10:55
  • No it isn't the problem, I try with one \ and I still have the same problem. –  Mar 21 '17 at 11:56

1 Answers1

1

If you look at the documentation for os.walk, you'll see that it does not simply return a list of files.

For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).

You should change your for loop to read:

...
for root, dirs, files in os.walk(r'C:\\Users\\user\\My_Test_Traces\\1000_Traces_npy'):
    for file in files:
        ...

However, it seems like you only want to parse the files in a single directory while os.walk is used to parse an entire directory tree. To only get the files in your current directory you can use os.listdir and os.path.isfile as recommended in this question

files = [f for f in os.listdir('/your/path') if os.path.isfile(f)]
Community
  • 1
  • 1
Chris Mueller
  • 6,490
  • 5
  • 29
  • 35
  • Thank you for your help, It gives me now this error: file.write(struct.pack(path_For_Binary_Files, t)) AttributeError: 'str' object has no attribute 'write' –  Mar 21 '17 at 12:20
  • I edit my question based on your answer. Could you please take a look at the edited question? –  Mar 21 '17 at 12:21
  • @user6652926 You need to pause for a minute and consider the error message before asking for help. `file` is a string in your code, `f` is the file you have opened for writing. – Chris Mueller Mar 21 '17 at 12:26