Python path and append

Seymore4Head Seymore4Head at Hotmail.invalid
Mon Apr 25 16:43:42 EDT 2016


I am using a test file that is only 3 lines:
Punjabi .Mp3
Big Lake (DVD) SWV.avi
Blue Balloon.AHC.RH.mkv

The program correctly appends an * to the end of the line, but then it
goes into a loop printing random looking stuff.

f = open('wout.txt', 'r+')
for line in f:
    if line=="":
        exit
    line=line[:-1]
    line=line+" *"
    f.write(line)
    print line
f.close()


On Mon, 25 Apr 2016 20:44:50 +0100, MRAB <python at mrabarnett.plus.com>
wrote:

>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 at python.org] On Behalf Of Seymore4Head
>> Sent: 25 April 2016 20:01
>> To: python-list at python.org
>> Subject: Re: Python path and append
>>
>> On Mon, 25 Apr 2016 18:24:02 -0000 (UTC), Rob Gaddi <rgaddi at highlandtechnology.invalid> wrote:
>>
>>>Seymore4Head wrote:
>>>
>>>> On Tue, 19 Apr 2016 18:29:38 -0400, Seymore4Head
>>>> <Seymore4Head at 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.
>>



More information about the Python-list mailing list