Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: Appending an asterisk to the end of each line Date: Wed, 6 Jul 2016 01:05:12 +0100 Lines: 65 Message-ID: References: <72a10a7f-c2a1-fb09-21cb-d6204d9a4f0a@mrabarnett.plus.com> <7ggonbtg4vhul20v11qvsiufcks1dvouop@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de z54iJ/tQtQQEuqW2YK+BCAPJUN9dxQj45Y1PXTp/qb6g== 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; 'encoded': 0.05; 'mrab': 0.05; 'filename': 0.07; 'indicated': 0.07; 'rename': 0.07; 'works.': 0.07; "'w')": 0.09; "'w',": 0.09; 'exception,': 0.09; 'exist.': 0.09; 'files:': 0.09; 'that).': 0.09; 'encoding': 0.15; "'r')": 0.16; "'r',": 0.16; '2016': 0.16; '>>on': 0.16; 'commented': 0.16; 'empty.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'renamed': 0.16; 'renames': 0.16; "would've": 0.16; 'wrote:': 0.16; "shouldn't": 0.18; 'creates': 0.18; '>>>': 0.20; 'posted': 0.21; '(the': 0.22; 'assuming': 0.22; 'tried': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'not.': 0.27; 'least': 0.27; 'specify': 0.27; "skip:' 10": 0.28; '(it': 0.29; 'subject:end': 0.29; 'raise': 0.29; 'url:mailman': 0.30; 'german': 0.32; 'received:84': 0.32; 'run': 0.33; 'problem': 0.33; 'url:python': 0.33; 'traceback': 0.33; 'open': 0.33; 'url:listinfo': 0.34; 'tue,': 0.34; 'file': 0.34; 'gets': 0.35; 'text': 0.35; 'replaced': 0.35; 'something': 0.35; 'comment': 0.35; 'but': 0.36; 'url:org': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'several': 0.38; 'skip:o 20': 0.38; 'subject:the': 0.39; 'received:192': 0.39; 'url:mail': 0.40; 'to:addr:python.org': 0.40; 'still': 0.40; 'called': 0.40; 'care': 0.60; "you'll": 0.61; 'ago.': 0.61; '>>>>>': 0.66; 'reuse': 0.66; 'jul': 0.72; 'square': 0.76; 'seymore4head': 0.84; 'subject:Appending': 0.84; 'joel': 0.91; 'suspicious': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.2 cv=QfYkhYTv c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=IkcTkHD0fZMA:10 a=pGLkceISAAAA:8 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=6U30xS2w1znLTkXMAxYA:9 a=6kGIvZw6iX1k4Y-7sg4_:22 a=yJM6EZoI5SlJf8ks9Ge_:22 a=7K4yPEzc-uA7A4xFz8XL:22 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.1 In-Reply-To: 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: <72a10a7f-c2a1-fb09-21cb-d6204d9a4f0a@mrabarnett.plus.com> <7ggonbtg4vhul20v11qvsiufcks1dvouop@4ax.com> Xref: csiph.com comp.lang.python:111158 On 2016-07-06 00:45, Seymore4Head wrote: > On Tue, 05 Jul 2016 19:29:21 -0400, Seymore4Head > wrote: > >>On Tue, 5 Jul 2016 19:15:23 -0400, Joel Goldstick >> wrote: >> >>>On Tue, Jul 5, 2016 at 7:03 PM, MRAB wrote: >>>> On 2016-07-05 23:05, 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. >>>>> >>>> Although it creates a file called "win_new.txt", it then renames it to >>>> "win.txt", so "win_new.txt" shouldn't exist. >>>> >>>> Of course, if there's already a file called "win_old.txt", then the first >>>> rename will raise an exception, and you'll have "win_new.txt" and the >>>> original "win.txt". >>>> >>>> -- >>>> https://mail.python.org/mailman/listinfo/python-list >>> >>>Why don't you comment out the renames, and see what happens? >> >>I really don't care if the filename gets renamed or not. I commented >>out the renames, but I still get a new file called win_new.txt and it >>is empty. >> >>The original is unchanged. > > I just tried this on a 3 line text file and it works. > > I am looking through the text file and have found at least two > suspicious characters. One is a German letter and the other is a > characters that has been replaced by a square symbol. > That suggests to me that it's an encoding problem (the traceback would've indicated that). Specify an encoding when you open the files: f_in = open('win.txt', 'r', encoding='utf-8') f_out = open('win_new.txt', 'w', encoding='utf-8') assuming that 'win.txt' is indeed encoded in UTF-8. (It might be something like ISO-8859-1 instead.)