1

samples folder has a bunch of samples a,b,c,d,e.

while saving the final output features_with_times to a file, I want it to be appended with name of the file it just processed.
I approached using Creating a new file, filename contains loop variable, python , and I did the following, but getting small error.

from __future__ import division

import librosa
import os
import numpy as np


test_src = 'samples/'

path_to_audios = [os.path.join(test_src, f) for f in os.listdir(test_src)]

for audio_path in path_to_audios:
    # blah ..
    # blah 

    # blah . . 
    # blah  . . 


    features_with_times= some_val 
    # np.savetxt('koo'+ str(k) + '.txt')

    print "saving mfcc features"
    np.savetxt('mfcc_flwts'+str(audio_path)+'.txt', features_with_times,newline ='\n', delimiter= '\t')

ERROR : IOError: [Errno 2] No such file or directory: 'mfcc_flwtssamples/abc.mp3.txt'

How to fix this ? how do _prevent the samples/ tag from coming in between. I know I can have names_to_append = [f for f in os.listdir(test_src)] which will save the names of the files present in the sample/ folder. to a list.

How do I pass these to the np.savetxt() step.

Newbie question.

UPDATE: I crude solution I came up with was to subtract the two strings:

a = 'samples/'
b = audio_path
val = b.replace(a,'')
np.savetxt('mfcc_flwts_'+str(val)+'.txt', features_with_times,newline ='\n', delimiter= '\t')

Is there a better way to achieve my solution.

UPDATE : 2 :

I am also able to save it to a folder of my choice as follows:

save_destination = 'outputss/'
    np.savetxt(os.path.join(save_destination,'mfcc_flwts_'+str(val)+'.txt'), features_with_times,newline ='\n', delimiter= '\t')
kRazzy R
  • 1,561
  • 1
  • 16
  • 44
  • What is your you expected filename/filepath? – wwii Feb 08 '18 at 16:31
  • if `samples` folder has audios `a` `b` `c`. after processing, outputs from np.savetxt should be `mfcc_flwts_a.txt` `mfcc_flwts_b.txt` `mfcc_flwts_c.txt` so I'll know which was generated from which file. – kRazzy R Feb 08 '18 at 16:34
  • one crude way to do would be two subtract two strings like: `a = 'samples/'` `>>> b = 'samples/aa.mp3'` `>>> b.replace(a,'')` `'aa.mp3'` and pass this^ to the str() part of np.savetxt – kRazzy R Feb 08 '18 at 16:40
  • also how do I save them to a specific path ? right now they just get saved to the working directory – kRazzy R Feb 08 '18 at 16:50

1 Answers1

1

Your problem is that path_to_audios contains the relative path of files in samples/<filename>, not just the filenames alone. One idea would be to change your loop a little, so you've got just the filenames available in the loop:

test_src = 'samples/'

filenames = os.listdir(test_src)
path_to_audios = [os.path.join(test_src, f) for f in filenames]

for fn, audio_path in zip(filenames, path_to_audios):
    # now you've got path and filename in parallel. If you need to discard the file ending since it's not ".txt",
    # split at the dot and take the first part only
    fn = fn.split('.')[0]

    print "saving mfcc features"
    np.savetxt('mfcc_flwts'+str(fn)+'.txt', features_with_times,newline ='\n', delimiter= '\t')

The last line saves your result in the working directory, and it's also an ugly way to write your filename. So we'd like to change it to...

    np.savetxt(
        os.path.join(your_target_path, 'mfcc_flwts{0}.txt'.format(fn)),
        features_with_times,
        newline ='\n',
        delimiter= '\t'
    )
ascripter
  • 5,665
  • 12
  • 45
  • 68