Search code examples
pythonlistappendnonetype

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


I am coding a script in python that automatically writes a file which you can use in the DecentSampler plugin, I am running into an error right now which I am not understanding.

        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)

I am getting the error

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

But if I try this:

noteGroups = ["test", "test"]

note = "A2"

noteGroups.append(note)
print(noteGroups)

It does function properly... Does anyone know why? And how I can fix it?


Solution

  • The problem is this line:

    noteGroups = noteGroups.append(note)
    

    append modifies the list in-place and then it returns None. Don't assign that None value back to the name of the original list. Just do:

    noteGroups.append(note)