简体   繁体   English

将处理后的文件名动态附加到输出 txt 文件

[英]dynamically append processed filename to output txt file

samples folder has a bunch of samples a,b,c,d,e .样本文件夹有一堆样本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.在将最终输出features_with_times保存到文件时,我希望它附加有它刚刚处理的文件的名称。
I approached using Creating a new file, filename contains loop variable, python , and I did the following, but getting small error.我接近使用创建新文件,文件名包含循环变量 python ,我做了以下,但得到小错误。

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'错误:IOError:[Errno 2] 没有这样的文件或目录:'mfcc_flwtssamples/abc.mp3.txt'

How to fix this ?如何解决这个问题? how do _prevent the samples/ tag from coming in between.如何 _prevent samples/标签介于两者之间。 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.我知道我可以有names_to_append = [f for f in os.listdir(test_src)]这将保存 sample/ 文件夹中存在的文件的名称。 to a list.到一个列表。

How do I pass these to the np.savetxt() step.我如何将这些传递给np.savetxt()步骤。

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 :更新: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')

Your problem is that path_to_audios contains the relative path of files in samples/<filename> , not just the filenames alone.您的问题是path_to_audios包含samples/<filename>中文件的相对路径,而不仅仅是文件名。 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'
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM