Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Random832 Newsgroups: comp.lang.python Subject: Re: Python path and append Date: Mon, 25 Apr 2016 16:28:32 -0400 Lines: 35 Message-ID: References: <27nshbp40p1llr231dqm31p754tvurkb8i@4ax.com> <02ushb9mntvtedeg5c7l33uhapt2j6nivu@4ax.com> <1461616112.2223232.589261113.1AB67247@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de ZLZIqQ72gpo9hU/tGlrGYA49wZ5dmrXbAqIpfI6enG5Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.05; 'modified': 0.05; 'exit': 0.07; 'f.close()': 0.07; 'cursor': 0.09; 'positioned': 0.09; 'received:internal': 0.09; 'message- id:@webmail.messagingengine.com': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'memory': 0.17; 'this:': 0.23; 'header:In-Reply- To:1': 0.24; 'mon,': 0.24; 'this.': 0.28; 'print': 0.30; 'that.': 0.30; 'problem': 0.33; 'file': 0.34; 'list,': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'thanks': 0.37; 'received:66': 0.38; 'version': 0.38; 'end': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'your': 0.60; "you'll": 0.61; 'header:Message-Id:1': 0.61; 'is.': 0.63; '(look': 0.84; 'aaa': 0.84; 'function)': 0.84; 'seymore4head': 0.84 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=jdXyJnAHB+MO0xRggnNTPktfdKg=; b=w+AcDc sQd2RoPZ0Rd/X1Ipl33BMPTQSnNqj2UTdu5XiZEQXahGW5GlRD/YpYasmZcri5PT VpSITFe36m5j6pfKycahk5uK8Uy6HG6hhz9XjJRYiMxotfBHVgHWEEgcKjpDTgvj /5MaQ/+hp2Wda8kW5knZ/QeZ6iSWgAezLJOD4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=jdXyJnAHB+MO0xR ggnNTPktfdKg=; b=pRycWDJu1HxHOSC/y9wkcl2iu8wrVgai1T/z7bsdiE5Taga vsd5Orf1JGMmkiYWZhC3kkr090iDHkV51ZD4iuEF4yip5CCJS3j4Aj+UDOjE4mhR GE7ZiaRoQM29uVbGSfcZdJMNDf48W8OUk0Gjgsd13Qqlm+nXfvlCKD5TJI2s= X-Sasl-Enc: 79yDjp7umGNimZxF0q4X+XatbhMm/akwEFT3B2YmF2Nq 1461616112 X-Mailer: MessagingEngine.com Webmail Interface - ajax-76f1c811 In-Reply-To: <02ushb9mntvtedeg5c7l33uhapt2j6nivu@4ax.com> 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: <1461616112.2223232.589261113.1AB67247@webmail.messagingengine.com> X-Mailman-Original-References: <27nshbp40p1llr231dqm31p754tvurkb8i@4ax.com> <02ushb9mntvtedeg5c7l33uhapt2j6nivu@4ax.com> Xref: csiph.com comp.lang.python:107627 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.