当我定义上面的变量时,为什么会出现 'undefined variable' 错误
Why am I getting a 'undefined variable' error when I have defined the variable above
整个文件
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()
这是我遇到错误的代码。
# 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")]
在我 运行 我的代码之后,在第 33 行它输出 'name label_dir is not defined' 即使我在前一行定义了它。
为什么会出现这个错误?
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():
您在 class 定义的顶层有可执行内容。这不一定是错误的,但通常情况下,您只希望在那里看到变量赋值和方法 def:s 。你确定你不是想说 def ImportData()
而不是 class ImportData()
吗?
(语言外行注意:是的,我知道def:s和赋值也是可执行的...)
整个文件
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()
这是我遇到错误的代码。
# 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")]
在我 运行 我的代码之后,在第 33 行它输出 'name label_dir is not defined' 即使我在前一行定义了它。
为什么会出现这个错误?
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():
您在 class 定义的顶层有可执行内容。这不一定是错误的,但通常情况下,您只希望在那里看到变量赋值和方法 def:s 。你确定你不是想说 def ImportData()
而不是 class ImportData()
吗?
(语言外行注意:是的,我知道def:s和赋值也是可执行的...)