Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107627
| From | Random832 <random832@fastmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Python path and append |
| Date | 2016-04-25 16:28 -0400 |
| Message-ID | <mailman.92.1461616116.32212.python-list@python.org> (permalink) |
| References | (3 earlier) <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> |
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.
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