Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107631
| 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 | <mailman.94.1461620307.32212.python-list@python.org> (permalink) |
| References | <smbdhb5kjje2oandna4vj5udku9gh1h9oa@4ax.com> <27nshbp40p1llr231dqm31p754tvurkb8i@4ax.com> <nflnc1$7a4$1@dont-email.me> <n5qshb5tmq4gk6nvqmad44lb523ouoiji5@4ax.com> <HE1PR07MB1356FDA50BB26CB85681F3E2F0620@HE1PR07MB1356.eurprd07.prod.outlook.com> <mailman.89.1461611345.32212.python-list@python.org> <02ushb9mntvtedeg5c7l33uhapt2j6nivu@4ax.com> <1461616112.2223232.589261113.1AB67247@webmail.messagingengine.com> <nfm2o7$j0h$1@ger.gmane.org> |
| 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 | <python-python-list@m.gmane.org> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <nfm2o7$j0h$1@ger.gmane.org> |
| X-Mailman-Original-References | <smbdhb5kjje2oandna4vj5udku9gh1h9oa@4ax.com> <27nshbp40p1llr231dqm31p754tvurkb8i@4ax.com> <nflnc1$7a4$1@dont-email.me> <n5qshb5tmq4gk6nvqmad44lb523ouoiji5@4ax.com> <HE1PR07MB1356FDA50BB26CB85681F3E2F0620@HE1PR07MB1356.eurprd07.prod.outlook.com> <mailman.89.1461611345.32212.python-list@python.org> <02ushb9mntvtedeg5c7l33uhapt2j6nivu@4ax.com> <1461616112.2223232.589261113.1AB67247@webmail.messagingengine.com> |
| Xref | csiph.com comp.lang.python:107631 |
Show key headers only | View raw
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Python path and append Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-04-19 18:29 -0400
Re: Python path and append Chris Angelico <rosuav@gmail.com> - 2016-04-20 08:38 +1000
Re: Python path and append Matthew Barnett <mrabarnett@mrabarnett.plus.com> - 2016-04-20 00:36 +0100
Re: Python path and append Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-04-25 14:10 -0400
Re: Python path and append Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-25 18:24 +0000
Re: Python path and append Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-04-25 15:00 -0400
RE: Python path and append Joaquin Alzola <Joaquin.Alzola@lebara.com> - 2016-04-25 19:08 +0000
Re: Python path and append Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-04-25 16:15 -0400
Re: Python path and append Random832 <random832@fastmail.com> - 2016-04-25 16:28 -0400
Re: Python path and append Peter Otten <__peter__@web.de> - 2016-04-25 23:38 +0200
Re: Python path and append Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-04-25 19:57 -0400
Re: Python path and append Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-25 19:31 +0000
Re: Python path and append MRAB <python@mrabarnett.plus.com> - 2016-04-25 20:44 +0100
Re: Python path and append Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-04-25 16:43 -0400
Re: Python path and append Steven D'Aprano <steve@pearwood.info> - 2016-04-26 11:51 +1000
Re: Python path and append Dan Sommers <dan@tombstonezero.net> - 2016-04-26 01:59 +0000
Re: Python path and append Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-04-26 18:23 +1200
Re: Python path and append boB Stepp <robertvstepp@gmail.com> - 2016-04-29 15:26 -0500
Re: Python path and append Steven D'Aprano <steve@pearwood.info> - 2016-04-30 11:44 +1000
Re: Python path and append John Gordon <gordon@panix.com> - 2016-04-25 21:26 +0000
Re: Python path and append Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-04-25 18:04 -0400
Re: Python path and append Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-04-25 20:03 -0400
Re: Python path and append Steven D'Aprano <steve@pearwood.info> - 2016-04-26 11:53 +1000
Re: Python path and append Seymore4Head <Seymore4Head@Hotmail.invalid> - 2016-04-26 22:56 -0400
Re: Python path and append Chris Angelico <rosuav@gmail.com> - 2016-04-27 13:06 +1000
Re: Python path and append Stephen Hansen <me+python@ixokai.io> - 2016-04-27 17:24 -0700
Re: Python path and append Chris Angelico <rosuav@gmail.com> - 2016-04-26 10:25 +1000
csiph-web