Trouble writing lines into file with line feeds- Python Newb

daniel.t.healy at gmail.com daniel.t.healy at gmail.com
Sun Dec 22 19:53:22 EST 2013


Overview: I'm attempting to read strings from a serial port. Each string ends with a carriage return and line feed. I want to write those strings to a file, like a log file. So, if I send P1 and the P2 on a new line, I would expect to open this file and find (line 1) P1 (line 2) P2. 

Problem: The file only contains P2. It always overwrites the first line. I can send 20 strings and the file will always contain the last string received.

Code:

#Import the serial module
import serial

#Open the serial port w/ settings
ser=serial.Serial(
	port="/dev/ttyUSB0", 
	baudrate=9600, 
	timeout=None)

#Establish a placeholder for the variable line
line=[]

#Print data received on the serial port after removing the CR and LF characters
while True:
        rawcode=ser.readline()
        codelog=open('/home/pi/avdms/codes.log','w')
        codelog.write(rawcode)
        codelog.close()



More information about the Python-list mailing list