遍历文件夹和子文件夹以查找特定文件

时间:2017-03-24 03:04:31

标签: python

我想遍历一个文件夹,然后它的子文件夹找到一个.wav文件并重命名它。我在下面使用此代码,但它会找到所有文件并打印出来。这里是文件夹final_Earthquake和许多子文件夹。我想逐个工作,为特定子文件夹中的文件提供特定标记。使用下面的代码,我无法找到来自.wav文件的子文件夹。

我想首先找到子文件夹中的文件,然后转到另一个子文件夹,依此类推。

 import os

rootdir = r'C:\Users\KUSHAL\Desktop\Final_Earthquake'

for subdir, dirs, files in os.walk(rootdir):
    for dire in dirs:
        for file in files:
         filepath = subdir+os.sep +file
         if filepath.endswith('.wav'):
          #do my stuff 

          print (dire + file)

0 个答案:

没有答案
相关问题