Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #62604

Re: 2nd Try: Trouble writing lines to file that include line feeds - Newbie

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <gary.herron@islandtraining.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'problem:': 0.07; 'subject:file': 0.07; 'string': 0.09; '#print': 0.09; 'dan': 0.09; 'modes': 0.09; 'parameter': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'times,': 0.14; 'carriage': 0.16; 'feed.': 0.16; 'overview:': 0.16; 'subject:writing': 0.16; 'true:': 0.16; 'truncate': 0.16; 'wrote:': 0.18; 'module': 0.19; 'file,': 0.19; 'settings': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'port.': 0.24; 'received:emailsrvr.com': 0.24; 'file.': 0.24; 'first,': 0.26; 'received:(smtp server)': 0.26; 'second': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'testing': 0.29; 'characters': 0.30; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; 'gary': 0.31; 'subject:that': 0.31; 'file': 0.32; 'quite': 0.32; 'open': 0.33; 'url:python': 0.33; 'call.': 0.33; 'something': 0.35; 'really': 0.36; "i'll": 0.36; 'url:org': 0.36; 'so,': 0.37; 'list': 0.37; 'clear': 0.37; '(i.e.,': 0.38; 'ends': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'read': 0.60; 'removing': 0.60; 'new': 0.61; 'url:3': 0.61; 'first': 0.61; 'chance': 0.65; 'close': 0.67; 'line,': 0.68; 'default': 0.69; 'serial': 0.72; 'behavior': 0.77; 'subject:2nd': 0.84
X-Virus-Scanned OK
Date Mon, 23 Dec 2013 00:33:24 -0800
From Gary Herron <gary.herron@islandtraining.com>
User-Agent Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
MIME-Version 1.0
To python-list@python.org
Subject Re: 2nd Try: Trouble writing lines to file that include line feeds - Newbie
References <f4f962d9-0e23-4b33-ba88-ce61afbb3418@googlegroups.com>
In-Reply-To <f4f962d9-0e23-4b33-ba88-ce61afbb3418@googlegroups.com>
Content-Type multipart/alternative; boundary="------------010802020102020806030405"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4540.1387788095.18130.python-list@python.org> (permalink)
Lines 127
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1387788095 news.xs4all.nl 2902 [2001:888:2000:d::a6]:42725
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:62604

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 12/22/2013 04:55 PM, 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()

First, that code is quite foolish:  If you want to write multiple lines 
to a file, open the file once before the loop, looped through all your 
writes, and then closed it after the loop has exited.:

codelog=open('/home/pi/avdms/codes.log','w')
while True:
         rawcode=ser.readline()
         codelog.write(rawcode)
         
codelog.close()


However, on the chance that you are writing this code for testing 
purposes, and you really do have a need to open a file, write something 
onto the end and then close the file, and that you are going to do this 
many times, I'll continue with this response:

    The default behavior of the "open" call is to *truncate* (i.e.,
    clear the  contents) and start writing at the beginning. The
    behavior you want is called *append*, and you get it with a 'wa' as
    the second parameter of the open call.


See http://docs.python.org/3/library/functions.html#open for a list of 
other modes available for the open call.

Gary Herron

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

2nd Try: Trouble writing lines to file that include line feeds - Newbie Dan Healy <daniel.t.healy@gmail.com> - 2013-12-22 16:55 -0800
  Re: 2nd Try: Trouble writing lines to file that include line feeds - Newbie Gary Herron <gary.herron@islandtraining.com> - 2013-12-23 00:33 -0800

csiph-web