尝试将字符串附加到列表时出现错误

When trying to append a string to a list I get an error

我正在 python 中编写一个脚本,该脚本会自动写入一个您可以在 DecentSampler 插件中使用的文件,我现在 运行 遇到了一个我不理解的错误。

        noteGroups = []
        firstGroup = True

        print(noteGroups)

        for file in files:          #Writes all the files in the pack.
            rfeS = ""

            strFile = str(file)

            rfe = strFile.split(".", 1)
            rfe.pop(-1)
            for rfeX in rfe:
                rfeS += rfeX
            filesSplit = rfeS.split("_", 1)

            note = filesSplit[0]
            rr = filesSplit[1]

            noteList = note.split("delimiter")
            print(noteList)

            if note not in noteGroups:
                noteGroups = noteGroups.append(note)
                print(noteGroups)
                if firstGroup:
                    dsp.write("\n    </group>")
                    firstGroup = False
                dsp.write("\n    <group>")
                dsp.write("\n      <sample path=\"" + dir + "/" + file + "\" volume=\"5dB\" rootNote=\"" + note + "\" loNote=\"" + note + "\" hiNote=\"" + note + "\" seqPosition=\"" + rr + "\" />")
            else:
                
                print(noteGroups)
                dsp.write("\n      <sample path=\"" + dir + "/" + file + "\" volume=\"5dB\" rootNote=\"" + note + "\" loNote=\"" + note + "\" hiNote=\"" + note + "\" seqPosition=\"" + rr + "\" />")

        print(noteGroups)

我遇到了错误

File "D:\Python\GUI-test\dshgui.py", line 109, in dspWrite
    if note not in noteGroups:
TypeError: argument of type 'NoneType' is not iterable

但如果我尝试这样做:

noteGroups = ["test", "test"]

note = "A2"

noteGroups.append(note)
print(noteGroups)

它运行正常... 有谁知道为什么?我该如何解决?

问题出在这一行:

noteGroups = noteGroups.append(note)

append 修改列表 in-place 然后它 returns None不要将 None 值重新分配给原始列表的名称。 只需:

noteGroups.append(note)