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


Groups > comp.lang.python > #6125 > unrolled thread

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

Started byChris Rebert <clp2@rebertia.com>
First post2011-05-24 01:58 -0700
Last post2011-05-24 01:58 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

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

FromChris Rebert <clp2@rebertia.com>
Date2011-05-24 01:58 -0700
SubjectRe: NEED HELP- read file contents, while loop to accept user input, and enter to exit
Message-ID<mailman.2008.1306227882.9059.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web