I’m writing to a text file using the code below. the setup is:
- read ‘\n’-terminated data off of the serial port.
- create a text file containing only one line of text.
_
import serial
ser = serial.Serial ( 2 , baudrate = 57600 , timeout = 1 )
AngleText = open ("data.txt" , "w")
while True:
line = ser.readline().strip('^\n\r')
print line
AngleText.write (str(line))
AngleText.flush()
ser.close()
The problem is, data gets appended to the text file. what I want is to have a text file containing only one line. How should I solve this? thanks!
One solution is to move the
openandclosestatements inside the loop so that each time you get data you overwrite the file. For example: