Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Dennis Lee Bieber Newsgroups: comp.lang.python Subject: Re: Python path and append Date: Mon, 25 Apr 2016 19:57:46 -0400 Organization: IISS Elusive Unicorn Lines: 76 Message-ID: References: <27nshbp40p1llr231dqm31p754tvurkb8i@4ax.com> <02ushb9mntvtedeg5c7l33uhapt2j6nivu@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de UyHtP200ftTtwLxxoDSb5AXuM6zDPMvqQgyU3akstRLw== 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; 'newline': 0.07; 'rename': 0.07; 'garbage': 0.09; 'input,': 0.09; 'message-id:@4ax.com': 0.09; 'overwrite': 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; 'file,': 0.15; '"\\n":': 0.16; '(relatively': 0.16; '2016': 0.16; 'fail,': 0.16; 'length,': 0.16; 'modify,': 0.16; 'operation.': 0.16; 'pair.': 0.16; 'pointer,': 0.16; 'reads,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'renames': 0.16; 'reposition': 0.16; 'write,': 0.16; 'pointer': 0.18; 'url:home': 0.18; 'input': 0.18; 'changes': 0.20; 'strip': 0.22; '(or': 0.23; 'wrote': 0.23; 'second': 0.24; 'mon,': 0.24; 'header:X-Complaints-To:1': 0.26; 'record': 0.29; 'i/o': 0.29; 'windows,': 0.29; 'read,': 0.29; 'starts': 0.29; 'print': 0.30; 'that.': 0.30; 'entry': 0.31; 'implement': 0.32; 'point': 0.33; 'third': 0.33; 'maintained': 0.33; 'open': 0.33; 'file': 0.34; 'gets': 0.35; 'next': 0.35; 'text': 0.35; 'attempt': 0.35; 'files,': 0.35; 'replace': 0.35; 'unknown': 0.35; 'but': 0.36; 'located': 0.36; 'lines': 0.36; 'mode': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'received:org': 0.37; 'charset:us- ascii': 0.37; 'starting': 0.37; 'doing': 0.38; 'files': 0.38; 'end': 0.39; 'test': 0.39; 'data': 0.39; "didn't": 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'entire': 0.61; 'making': 0.62; 'more': 0.63; 'records': 0.70; 'physical': 0.72; '(look': 0.84; 'seymore4head': 0.84; 'xerox': 0.84; 'dennis': 0.91; 'hassle': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: adsl-108-79-218-66.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES 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> Xref: csiph.com comp.lang.python:107633 On Mon, 25 Apr 2016 16:15:15 -0400, Seymore4Head declaimed the following: >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() > > > >I did notice that it wrote the 3 lines of test file but it didn't >append the * after the third entry and it starts printing garbage >after that. > Unless Python (or the C runtime) have implement Xerox CP/V "UPDATE" file mode*, after the first line is read, the I/O pointer is located at the end of that line. You now attempt to write the modified line, which will overwrite parts of the second line (note that you are not writing new-lines). After that write, you are positioned at some point unknown (relatively speaking, and do a read operation starting at where the write ended. Any I/O operation that changes the length of the data is going to fail, even if you reposition (look up "seek()" and "tell()") On Windows, where the physical line-ending is , but gets transformed to just by Python text reads, you are stripping the , adding " *". If the next record is the same length, that " *" on write will replace the pair. For the type of operation you are performing, on text files, you really need to go through the hassle of renames and copies... That is, rename the input file to some temporary name, open it for read, open the original name for write, and then read input, modify, write output... OR -- if the files are short enough, just read the entire file, and process it in memory... fupdate = open("theFile.txt", "r+") fdata = fupdate.readlines() # doing piecemeal, rather than making a one-liner for l, ln in enumerate(fdata): # strip new-line IF present if ln[-1] == "\n": ln = ln[:-1] fdata[l] = ln + " *" # join new data with newline fdata = "\n".join(fdata) #rewind the file fupdate.seek(0) fupdate.write(fdata) fupdate.close() * UPDATE mode maintained two I/O pointers: a read pointer and write pointer, and required one to read one or more records before doing a write operation. As long as the data (record) size did not change it allowed for Read/Modify/Write of records. -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/