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


Groups > comp.lang.python > #6125

Re: NEED HELP- read file contents, while loop to accept user input, and enter to exit

References <BANLkTinh6V3-DtdU5davGdP5a53a2eDF_Q@mail.gmail.com>
Date 2011-05-24 01:58 -0700
Subject Re: NEED HELP- read file contents, while loop to accept user input, and enter to exit
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.2008.1306227882.9059.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, May 24, 2011 at 1:31 AM, Cathy James <nambo4jb@gmail.com> wrote:
> dear mentor,
>
> I need help with my code:
<snip>

In addition to what others have already said...

> please see my attempt below and help:
>
> #1) open file and display current file contents:
> f = open ('c:/testing.txt'', 'r')
> f.readlines()
> #2)  and 3) use while loop  to write user input to file, save to file, close
> when press enter:
> while True:
>     s = input ('enter name: ').strip()
>     f = open ('c:/testing.txt', 'a')
>     if f.writable():

Since you *just* opened the file in append mode, this condition will
*always* be true (append mode implies writability), so your `else`
clause will *never* be executed.

>         f.write(s)
>         break
>     else:
>         f = open ('c:/testing.txt', 'r')
>         f.readlines()
>         for line in f:
>             print (line)

Similar beginner questions would be best directed to Python's Tutor
mailinglist: http://mail.python.org/mailman/listinfo/tutor

Cheers,
Chris

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


Thread

Re: NEED HELP- read file contents, while loop to accept user input, and enter to exit Chris Rebert <clp2@rebertia.com> - 2011-05-24 01:58 -0700

csiph-web