Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107625

Re: Python path and append

From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re: Python path and append
Date 2016-04-25 20:44 +0100
Message-ID <mailman.90.1461613684.32212.python-list@python.org> (permalink)
References (1 earlier) <27nshbp40p1llr231dqm31p754tvurkb8i@4ax.com> <nflnc1$7a4$1@dont-email.me> <n5qshb5tmq4gk6nvqmad44lb523ouoiji5@4ax.com> <HE1PR07MB1356FDA50BB26CB85681F3E2F0620@HE1PR07MB1356.eurprd07.prod.outlook.com> <f144fd58-c3ad-409b-31af-cac4f7133f8d@mrabarnett.plus.com>

Show all headers | View raw


On 2016-04-25 20:08, Joaquin Alzola wrote:
> Strip() = white spaces.
> Description
> The method strip() returns a copy of the string in which all chars have been stripped from the beginning and the end of the string (default whitespace characters).
>
> Use to remove return carriage--> line[:-1]
>
1. In the file it might be a linefeed, or a carriage return, or a
carriage return followed by a linefeed, depending on the operating
system. Python translates it to a linefeed "\n" (or 'newline') on
reading.

2. It's possible that the last line doesn't end have a line ending, so
line[:-1] could be removing some other character. It's safer to use
line.rstrip("\n").

> -----Original Message-----
> From: Python-list [mailto:python-list-bounces+joaquin.alzola=lebara.com@python.org] On Behalf Of Seymore4Head
> Sent: 25 April 2016 20:01
> To: python-list@python.org
> Subject: Re: Python path and append
>
> On Mon, 25 Apr 2016 18:24:02 -0000 (UTC), Rob Gaddi <rgaddi@highlandtechnology.invalid> wrote:
>
>>Seymore4Head wrote:
>>
>>> On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head
>>> <Seymore4Head@Hotmail.invalid> wrote:
>>>
>>> I am going to forget using a directory path.
>>> I would like to take the file win.txt and append a space and the *
>>> symbol.
>>>
>>> f = open('win.txt', 'r+')
>>> for line in f:
>>>     f.read(line)
>>>     f.write(line+" *")
>>>
>>> This doesn't work.  Would someone fix it please?  It is for a task I
>>> am trying to accomplish just for a home task.
>>
>>"for line in f:" already means "make the variable line equal to each
>>line in f sequentially".  f.read is both superfluous and also doesn't
>>do that.  Leave it out entirely.
>>
>>The next problem you'll have is that iterating over the lines of the
>>file leaves the newline at the end of line, so your * will end up on
>>the wrong line.
>>
>>Do yourself a favor:
>>https://docs.python.org/3/tutorial/inputoutput.html
>>isn't very long.
>
> I was reading that.  I have read it before.  I don't use python enough to even remember the simple stuff.  Then when I try to use if for something simple I forget how.
>
> f = open('wout.txt', 'r+')
> for line in f:
>     line=line.strip()
>     f.write(line+" *")
> f.close()
>
> Still broke.  How about just telling me where I missed?  Please?
> --
> https://mail.python.org/mailman/listinfo/python-list
> This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt.
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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