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

Chris Rebert clp2 at rebertia.com
Tue May 24 04:58:25 EDT 2011


On Tue, May 24, 2011 at 1:31 AM, Cathy James <nambo4jb at 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



More information about the Python-list mailing list