1. 程式人生 > 其它 >python遍歷目錄,獲取指定檔案

python遍歷目錄,獲取指定檔案

技術標籤:python遍歷目錄

1 需求

遍歷指定資料夾下的wav檔案,取出後做其他處理

2 程式碼

'''
功能:讀取指定資料夾下的所有wav檔案

'''

import os
path=r'E:\TWK_LanguageRecognition\CommandLineCutting\voice\複母音'

filter=[".wav"] #設定過濾後的檔案型別 當然可以設定多個型別

def all_path(dirname):

    result = []#所有的檔案

    for maindir, subdir, file_name_list in os.
walk(dirname): for filename in file_name_list: apath = os.path.join(maindir, filename)#合併成一個完整路徑 ext = os.path.splitext(apath)[1] # 獲取檔案字尾 [0]獲取的是除了檔名以外的內容 if ext in filter: result.append(apath) return result filenames=all_path(path)
for filename in filenames: print(filename) # cmd_command = "activate pytorch_gpu1 && python ExtremePointFrequency.py -i {0} --maxnum -15".format(filename) # os.system(cmd_command)

3 結果

在這裡插入圖片描述