Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Python path and append Date: Mon, 25 Apr 2016 23:38:10 +0200 Organization: None Lines: 60 Message-ID: References: <27nshbp40p1llr231dqm31p754tvurkb8i@4ax.com> <02ushb9mntvtedeg5c7l33uhapt2j6nivu@4ax.com> <1461616112.2223232.589261113.1AB67247@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de SHQq8QbvsYBwRHDOtwOAYgiUPCGc0aR8HzVaCL7b/vHg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'modified': 0.05; 'exit': 0.07; 'f.close()': 0.07; 'though:': 0.07; 'cursor': 0.09; 'positioned': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'alpha': 0.15; '"*"': 0.16; 'gamma': 0.16; 'magic': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'memory': 0.17; '>>>': 0.20; 'leave': 0.23; 'this:': 0.23; 'import': 0.24; 'mon,': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'this.': 0.28; 'cat': 0.29; 'print': 0.30; 'that.': 0.30; 'problem': 0.33; 'file': 0.34; 'but': 0.36; 'too': 0.36; 'list,': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'version': 0.38; 'end': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'received:de': 0.40; 'your': 0.60; "you'll": 0.61; 'details': 0.62; 'is.': 0.63; 'more': 0.63; '(look': 0.84; 'aaa': 0.84; 'function)': 0.84; 'seymore4head': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd833c.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <27nshbp40p1llr231dqm31p754tvurkb8i@4ax.com> <02ushb9mntvtedeg5c7l33uhapt2j6nivu@4ax.com> <1461616112.2223232.589261113.1AB67247@webmail.messagingengine.com> Xref: csiph.com comp.lang.python:107631 Random832 wrote: > On Mon, Apr 25, 2016, at 16:15, Seymore4Head wrote: >> Thanks for the tip. >> >> Still broke. :( >> >> f = open('wout.txt', 'r+') >> for line in f: >> if line=="": >> exit >> line=line[:-1] >> line=line+" *" >> f.write(line) >> print line >> f.close() > > Your problem is that after you read the first line, your file "cursor" > is positioned after the end of that line. So when you write the modified > version of the line, it ends up after that. And then when you write it, > the cursor is wherever the end of that is. > > So if you start with this: > AAA > BBB > CCC > > You'll end up with this: > AAA > AAA* [this overwrites "BBB_C" with "AAA*_" if _ is the line break] > CC > CC* > > There's no good way around this. You can either read the whole file into > memory at once into a list, then rewind (look at the seek function) and > write the lines out of the list, or you can write to a *different* file > than the one you're reading. You can leave the details to python though: $ cat sample.txt alpha beta gamma $ python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import fileinput >>> for line in fileinput.input("sample.txt", inplace=True): ... print line.rstrip("\n"), "*" ... >>> $ cat sample.txt alpha * beta * gamma * Too much magic for my taste, but the OP might like it.