[英]Why am I getting a 'undefined variable' error when I have defined the variable above
Entire file整个文件
import os
import re
import pandas as pd
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
import librosa
class ImportData():
filename = "fma_metadata/tracks.csv"
tracks = pd.read_csv(filename, header=2, low_memory=False)
tracks_array = tracks.values
track_id = tracks_array[: , 0]
track_genre = tracks_array[:, 40]
track_id = track_id.reshape(track_id.shape[0], 1)
track_genre = track_genre.reshape(track_genre.shape[0], 1)
# fma dataset
song_folder = "fma_small"
sub_dirs = [o for o in os.listdir("fma_small")
if os.path.isdir(os.path.join("fma_small",o))]
count = 0
print("Converting .mp3s into MEL spectograms")
for d in sub_dirs:
label_dir = os.path.join(song_folder, d)
file_names = [os.path.join(label_dir, f) for f in os.listdir(label_dir) if f.endswith(".mp3")]
ImportData()
Here is my code where I am experiencing the error.这是我遇到错误的代码。
# fma dataset
song_folder = "fma_small"
sub_dirs = [o for o in os.listdir("fma_small")
if os.path.isdir(os.path.join("fma_small",o))]
count = 0
for d in sub_dirs:
label_dir = os.path.join(song_folder, d)
file_names = [os.path.join(label_dir, f) for f in os.listdir(label_dir) if f.endswith(".mp3")]
After I run my code, on line 33 it outputs 'name label_dir is not defined' even though I have it defined on the previous line.在我运行我的代码后,在第 33 行它输出'name label_dir is not defined',即使我在前一行定义了它。
Why is this error occurring?为什么会出现这个错误?
Exception has occurred: NameError
name 'label_dir' is not defined
File "D:\FinalProject\import_data.py", line 44, in <listcomp>
file_names = [os.path.join(label_dir, f) for f in os.listdir(label_dir) if f.endswith(".mp3")]
File "D:\FinalProject\import_data.py", line 44, in ImportData
file_names = [os.path.join(label_dir, f) for f in os.listdir(label_dir) if f.endswith(".mp3")]
File "D:\FinalProject\import_data.py", line 12, in <module>
class ImportData():
You have executable stuff at the top level of a class definition.您在 class 定义的顶层有可执行的东西。 That it not necessarily wrong but, normally, you'd only expect to see variable assignments and method def:s there.这不一定是错的,但通常你只希望在那里看到变量赋值和方法 def:s。 Are you sure you didn't mean to say def ImportData()
instead of class ImportData()
?你确定你不是说def ImportData()
而不是class ImportData()
吗?
(Note to language laywers: Yes, I know that def:s and assignments are also executable...) (语言专家注意:是的,我知道 def:s 和 assignments 也是可执行的......)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.