特殊的eyed3类型成为python的非类型

2024-06-07 14:39:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一个小脚本,将我所有的.flac文件从dap移动到Artister子文件夹,以便从计算机上轻松访问。我在Ubuntu上使用了python3和诸如eyed3和os之类的libs。而且它在近30首歌曲(2位艺术家)中表现良好。 下面是代码:

    import os
    import eyed3

    os.chdir("/home/user/Music")
    cwd = os.getcwd()

    # print the current directory
    print("Current working directory is:", cwd)

    listOfFiles = os.listdir()
    artists = []

    # print(listOfFiles)

    try:
        for file in listOfFiles:
            aFile = eyed3.load(file)
            artist = aFile.tag.artist
            if artist not in a:
                artists.append(artist)
                os.mkdir(artist)
            os.rename(cwd + '/' + file, cwd + '/' + artist + '/' + file)
    except:
        print("not a file")

    print(artists)

它创建了两个文件夹。 但当我试图在我的Windows PC上运行此脚本时,它抛出了一个异常并打印“不是文件”。 以下是不带try/except的控制台输出:

Traceback (most recent call last):
  File "D:\GitHub\Song-Sorter\main.py", line 21, in <module>
    artist = aFile.tag.artist
AttributeError: 'NoneType' object has no attribute 'tag'

所以我尝试了这个(在artist = aFile.tag.artist之后的下一行):

print(type(artist))

其输出为:

<class 'NoneType'>

有什么问题?不明白


Tags: 文件inimport脚本文件夹artistostag

热门问题