如何将文件名以某种格式传递到Python中的列表?

如何将文件名以某种格式传递到Python中的列表?

我正在关注本教程:http://nbviewer.jupyter.org/github/amsehili/audio-segmentation-by-classification-tutorial/blob/master/multiclass_audio_segmentation.ipynb
在教程的块[8]中有:

train_data = {}
train_data["silence"] = ["silence_1.wav", "silence_2.wav",   "silence_3.wav"]  
train_data["speech"]  = ["speech_1.wav", "speech_2.wav", "speech_3.wav", "speech_4.wav", "speech_5.wav"]

即我们传递一个文件夹中包含的 .wav 文件的名称;

我想训练/传递我自己的音乐片段,而不是沉默/言语。因此,我使用以下方法将一首音乐切成更小的片段:

ffmpeg -i hm1.mp3 -c copy -map 0 -segment_time 1 -f segment hm1_%03d.mp3

这生成了 120 个1 second long mp3 chunks原始文件。我使用以下命令保存了这些 1 秒块的文件名

ls >filenames.txt  

不可能"abc.wav" ,以如上所示的格式键入 120 来传递生成的音乐位的所有 120+ 个文件名。

答案1

使用 Python 来构建列表?

$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = ["hm1_{:03d}.mp3".format(i) for i in range(120)]
>>> a
['hm1_000.mp3', 'hm1_001.mp3', 'hm1_002.mp3', 'hm1_003.mp3', 'hm1_004.mp3', 'hm1_005.mp3', 'hm1_006.mp3', 'hm1_007.mp3', 'hm1_008.mp3', 'hm1_009.mp3', 'hm1_010.mp3', 'hm1_011.mp3', 'hm1_012.mp3', 'hm1_013.mp3', 'hm1_014.mp3', 'hm1_015.mp3', 'hm1_016.mp3', 'hm1_017.mp3', 'hm1_018.mp3', 'hm1_019.mp3', 'hm1_020.mp3', 'hm1_021.mp3', 'hm1_022.mp3', 'hm1_023.mp3', 'hm1_024.mp3', 'hm1_025.mp3', 'hm1_026.mp3', 'hm1_027.mp3', 'hm1_028.mp3', 'hm1_029.mp3', 'hm1_030.mp3', 'hm1_031.mp3', 'hm1_032.mp3', 'hm1_033.mp3', 'hm1_034.mp3', 'hm1_035.mp3', 'hm1_036.mp3', 'hm1_037.mp3', 'hm1_038.mp3', 'hm1_039.mp3', 'hm1_040.mp3', 'hm1_041.mp3', 'hm1_042.mp3', 'hm1_043.mp3', 'hm1_044.mp3', 'hm1_045.mp3', 'hm1_046.mp3', 'hm1_047.mp3', 'hm1_048.mp3', 'hm1_049.mp3', 'hm1_050.mp3', 'hm1_051.mp3', 'hm1_052.mp3', 'hm1_053.mp3', 'hm1_054.mp3', 'hm1_055.mp3', 'hm1_056.mp3', 'hm1_057.mp3', 'hm1_058.mp3', 'hm1_059.mp3', 'hm1_060.mp3', 'hm1_061.mp3', 'hm1_062.mp3', 'hm1_063.mp3', 'hm1_064.mp3', 'hm1_065.mp3', 'hm1_066.mp3', 'hm1_067.mp3', 'hm1_068.mp3', 'hm1_069.mp3', 'hm1_070.mp3', 'hm1_071.mp3', 'hm1_072.mp3', 'hm1_073.mp3', 'hm1_074.mp3', 'hm1_075.mp3', 'hm1_076.mp3', 'hm1_077.mp3', 'hm1_078.mp3', 'hm1_079.mp3', 'hm1_080.mp3', 'hm1_081.mp3', 'hm1_082.mp3', 'hm1_083.mp3', 'hm1_084.mp3', 'hm1_085.mp3', 'hm1_086.mp3', 'hm1_087.mp3', 'hm1_088.mp3', 'hm1_089.mp3', 'hm1_090.mp3', 'hm1_091.mp3', 'hm1_092.mp3', 'hm1_093.mp3', 'hm1_094.mp3', 'hm1_095.mp3', 'hm1_096.mp3', 'hm1_097.mp3', 'hm1_098.mp3', 'hm1_099.mp3', 'hm1_100.mp3', 'hm1_101.mp3', 'hm1_102.mp3', 'hm1_103.mp3', 'hm1_104.mp3', 'hm1_105.mp3', 'hm1_106.mp3', 'hm1_107.mp3', 'hm1_108.mp3', 'hm1_109.mp3', 'hm1_110.mp3', 'hm1_111.mp3', 'hm1_112.mp3', 'hm1_113.mp3', 'hm1_114.mp3', 'hm1_115.mp3', 'hm1_116.mp3', 'hm1_117.mp3', 'hm1_118.mp3', 'hm1_119.mp3']

相关内容