Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: cs@zip.com.au Newsgroups: comp.lang.python Subject: Re: Appending an asterisk to the end of each line Date: Wed, 6 Jul 2016 15:35:13 +1000 Lines: 63 Message-ID: References: <20160706053513.GA3675@cskk.homeip.net> Reply-To: python-list@python.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed X-Trace: news.uni-berlin.de 4eny7RJXBsAhJFBvLQJhsgICjbhgVPU6URdWLKTUs8hw== 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; 'lines,': 0.05; 'lines.': 0.07; 'newline': 0.07; 'trailing': 0.07; 'truncated': 0.07; 'cc:addr:python-list': 0.09; '%r"': 0.09; "'w')": 0.09; 'eat': 0.09; 'python': 0.10; 'file,': 0.15; "'r')": 0.16; '(and,': 0.16; '>on': 0.16; 'count)': 0.16; 'empty.': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'imo,': 0.16; 'message- id:@cskk.homeip.net': 0.16; 'people)': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:io': 0.16; 'received:optusnet.com.au': 0.16; 'received:psf.io': 0.16; 'received:syd.optusnet.com.au': 0.16; 'record;': 0.16; 'rudely': 0.16; 'simpson': 0.16; 'skip:> 20': 0.16; 'somehow.': 0.16; 'wrote:': 0.16; 'creates': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'posted': 0.21; 'cheers,': 0.22; 'cc:no real name:2**0': 0.22; '(or': 0.23; 'this:': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "skip:' 10": 0.28; 'subject:end': 0.29; 'termination': 0.29; 'character': 0.29; 'raise': 0.29; 'there.': 0.30; 'code': 0.30; 'version,': 0.30; 'says': 0.32; 'run': 0.33; 'usually': 0.33; 'consist': 0.33; 'received:com.au': 0.33; 'file': 0.34; 'list': 0.34; 'gives': 0.35; 'text': 0.35; 'skip:> 10': 0.35; 'knowledge': 0.35; 'something': 0.35; 'but': 0.36; 'lines': 0.36; "wasn't": 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; 'several': 0.38; 'files': 0.38; 'data': 0.39; 'whatever': 0.39; 'subject:the': 0.39; 'some': 0.40; 'from:no real name:2**0': 0.60; 'secure': 0.60; 'your': 0.60; 'ago.': 0.61; 'personally': 0.61; 'programs': 0.62; 'skip:n 10': 0.62; 'cameron': 0.66; 'reuse': 0.66; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'to:addr:yahoo.com': 0.80; 'reply-to:addr:python.org': 0.84; 'seymore4head': 0.84; 'subject:Appending': 0.84; 'faith': 0.91 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.1 cv=EfU1O6SC c=1 sm=1 tr=0 a=kPLexIa+XrsL4mdc8kxeNA==:117 a=kPLexIa+XrsL4mdc8kxeNA==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=kj9zAlcOel0A:10 a=cAmyUtKerLwA:10 a=8AHkEIZyAAAA:8 a=vrnE16BAAAAA:8 a=msOMJo8cSidkfj3DxIMA:9 a=CjuIK1q_8ugA:10 a=7K4yPEzc-uA7A4xFz8XL:22 a=D4O83h6meWPcCEhhlUO6:22 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: <20160706053513.GA3675@cskk.homeip.net> X-Mailman-Original-References: Xref: csiph.com comp.lang.python:111170 On 05Jul2016 21:37, Python List wrote: >On 07/05/2016 03:05 PM, Seymore4Head wrote: >>import os >> >>f_in = open('win.txt', 'r') >>f_out = open('win_new.txt', 'w') >> >>for line in f_in.read().splitlines(): >> f_out.write(line + " *\n") >> >>f_in.close() >>f_out.close() >> >>os.rename('win.txt', 'win_old.txt') >>os.rename('win_new.txt', 'win.txt') >> >>I just tried to reuse this program that was posted several months ago. >>I am using a text flie that is about 200 lines long and have named it >>win.txt. The file it creates when I run the program is win_new.txt >>but it's empty. Put a counter in your loop: count = 0 for line in f_in.read().splitlines(): f_out.write(line + " *\n") count += 1 print("count =", count) Check that it says 200 (or whatever number you expect). >Not your problem, but you can simplify your read/write loop to: > >for line in f_in: > f_out.write(line[:-1] + ' *\n') > >The 'line[:-1]' expression gives you the line up to but not including the trailing newline. >Alternately, use: f_out.write(line.rstrip() + ' *\n') Importantly for this version, every line _MUST_ have a trailing newline. Personally that is what I require of my text files anyway, but some dubious tools (and, IMO, dubious people) make text files with no final newline. For such a file the above code would eat the last character because we don't check that a newline is there. I take a hard line on such files and usually write programs that look like this: for line in f_in: if not line.endswith('\n'): raise ValueError("missing final newline on file, last line is: %r" (line,)) f_out.write(line[:-1] + ' *\n') Then one can proceed secure in the knowledge that the data are well formed. I consider the final newline something of a termination record; without it I have no faith that the file wasn't rudely truncated somehow. In other words, I consider a text file to consist of newline-terminated lines, not newline-separated lines. Cheers, Cameron Simpson