简体   繁体   English

为什么我在上面定义了变量时会收到“未定义的变量”错误

[英]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.

相关问题 为什么在定义变量时出现名称错误? - Why am I getting a name error when I defined the variable? 为什么我得到变量未定义错误? - Why am I getting the variable not defined error? 当我尝试引用以前在Python中定义的文件变量时,为什么会出现NameError? - Why am I getting a NameError when I try to reference to a file variable I have previously defined in Python? 为什么我收到错误消息,说未定义变量? - Why am I getting an error saying variable not defined? 为什么在尝试用python编程堆栈时将局部变量声明为全局变量时出现局部变量错误? - why am I getting an error of local variable when I have declared it as a global variable, whilst trying to program a stack in python? 为什么会出现错误“赋值之前引用本地变量”或“未定义全局变量”的错误? - Why am I getting either error “local variable referenced before assignment” or “global variable not defined”? 为什么我没有得到这个变量的索引错误? - Why i am getting not index error with this variable? Scrapy项目错误:实际上我已定义此变量时为“ undefined variable” - Scrapy project error: “undefined variable” when actually I have defined this variable 当我在函数中定义变量名时,为什么会出现 NameError? - Why am I getting a NameError when I've defined my variable name in a function? 为什么我在 function 中使用局部变量时出现错误,但它是全局变量 - Why am I getting an error that I'm using a local variable inside a function when it is a global variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM