import serial from time import sleep ser = serial.Serial('/dev/ttyACM0', 38400) sleep(1) file = open("dataset", "a") file.write("[") for i in range(1000): s = ser.readline() print(s) file.write(str(s)) file.write("]") file.close()