Search code examples
pythonlistappendattributeerrornonetype

failed to add item in a list : 'none type' object has no attribute 'append'


i try the following code in python 3.7 to acquire data from various sensor, and then store them on a MySQL database remotely on a NAS.

dataset = []
try:
    while count<max:
        humidite, temp = Adafruit_DHT.read_retry(sensor, pin)
        time.sleep(2)
        if humidite is not None and temp is not None:
            print("avant ", dataset)
            #print("La temperature est de : {0:0.1f}*C Humidité de : {1:0.1f}%".format(temp, humidite))
            dataset = dataset.append((time.ctime(),'lieu',temp,humidite))
            print("apres ", dataset)
            count+=1
        else:
            print("Lecture des informations impossible")
except KeyboardInterrupt:
    print("End of work")

dbase = get_connect()
add_data(dataset)
dbase.close()

I add some print to see "dataset". The loop failed at second pass (count=1) and the result of dataset from the 1st pass was:

avant []
apres None

Is there something wrong with the .append method?


Solution

  • Change this line

    dataset = dataset.append((time.ctime(),'lieu',temp,humidite))
    

    to

    dataset.append((time.ctime(),'lieu',temp,humidite))
    

    The method list.append is an in-place operation, and returns None which you then assign back over your list.