2nd Try: Trouble writing lines to file that include line feeds - Newbie
Dan Healy
daniel.t.healy at gmail.com
Sun Dec 22 22:09:34 EST 2013
Solved. Simply change the 'w' from the file.open method to an 'a' for append
On Sunday, December 22, 2013 7:55:28 PM UTC-5, Dan Healy wrote:
> 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)
>
>
>
> #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