【问题标题】:I get this error : [Errno 21] Is a directory:我收到此错误:[Errno 21] 是一个目录:
【发布时间】:2021-03-20 10:04:43
【问题描述】:
# Save directory path in 'path'
path = r'---path '

# Declare a dummy Numpy array (row vector)
result_array = np.empty([1,54])

# Create a list of audio file names 'file_list'
file_list = os.listdir(path)

i=0

for filename in file_list:
    
    # Read WAV file. 'rosa.core.load' returns sampling frequency in 'fs' and audio signal in 'sig'
    sig, fs = rosa.core.load(path + '\\' + file_list[i], sr=None)
    
    # Calculate the average mfcc (utterance-level features) using 'rosa.feat.mfcc()' and 'np.mean' method. '.T' transposes the rows and columns. 'axis=0' indicates average is calculated column-wise
    avg_mfcc_feat = np.mean(rosa.feature.mfcc(y=sig, sr=fs, n_mfcc=26).T,axis=0)
    
    # Calculate the standard deviation of mfcc (utterance-level features) using 'rosa.feat.mfcc()' and 'np.std' method. '.T' transposes the rows and columns. 'axis=0' indicates average is calculated column-wise
    std_mfcc_feat = np.std(rosa.feature.mfcc(y=sig, sr=fs, n_mfcc=26).T,axis=0)
    
    # Calculate the average zero crossing rate (utterance-level feature) using 'rosa.feat.zero_crossing_rate()' and 'np.mean' method. '.T' transposes the rows and columns. 'axis=0' indicates average is calculated column-wise
    zcross_feat = rosa.feature.zero_crossing_rate(sig)
    avg_zcross_feat = np.mean(rosa.feature.zero_crossing_rate(y=sig).T,axis=0)
    
    # Append the three 1D arrays into a single 1D array called 'feat'.
    feat0 = np.append(avg_mfcc_feat, std_mfcc_feat, axis=0)
    
    feat1 = np.append(feat0, avg_zcross_feat, axis=0)
    
    # Save emotion label from file name. 'path' contains directory's address, 'file_list' contains file name, and '\\' joins the two to form file's address
    label = os.path.splitext(os.path.basename(path + '\\' + file_list[i]))[0].split('-')[2]
    
    # Create a new Numpy array 'sample' to store features along with label
    sample = np.insert(feat1, obj=53, values=label)
    
    result_array = np.append(result_array, sample)
    
    i+=1

# Print out the 1D Numpy array
result_array

我在尝试读取内容并循环遍历 Ravdess 数据集的子目录时遇到此错误。有人可以告诉我我做错了什么吗?任何帮助都深表感谢。谢谢!

【问题讨论】:

    标签: python audio processing


    【解决方案1】:

    1. os.listdir() 列出目录的条目,即它包含的文件和目录。如果要递归包含path 下的所有文件和文件夹,请使用os.walk()

    要仅使用来自os.listdir() 的文件,然后使用os.path.isfile() 添加检查:

    如果 pathexisting 常规文件,则返回 True。这遵循符号链接,因此islink()isfile() 对于同一路径都可以为真。

    for filename in file_list:
        if not os.path.isfile(os.path.join(path, filename)):
            # not a file, skip it
            continue
        # rest of your code here
        sig, fs = rosa.core.load(...)
        ...
    

    2.for-loop 中,您有两种情况可以使用path + '\\' + file_list[i] - 这应该只是path + '\\' + filename,或者更好的是os.path.join(path, filename)

    • 如果您想要使用文件名迭代的索引,请使用for i, filename in enumerate(file_list): 而不是手动递增i
    • 而不是多次重复os.path.join(path, filename),而是将它放在循环开始的变量中:
      for filename in file_list:
          filepath = os.path.join(path, filename)
          if not os.path.isfile(filepath):
              continue
      
          sig, fs = rosa.core.load(filepath, sr=None)
          ...
      

    【讨论】:

      猜你喜欢
      • 2022-12-07
      • 2023-03-10
      • 2018-06-20
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 2021-11-01
      • 2020-11-20
      • 2014-07-09
      相关资源
      最近更新 更多