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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news.mixmin.net!news.unit0.net!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.010
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'beginner': 0.04; 'mode,': 0.07; 'subject:while': 0.07; 'append': 0.09; 'am,': 0.14; 'subject:file': 0.14; 'wrote:': 0.14; "'r')": 0.16; '*always*': 0.16; '*never*': 0.16; 'executed.': 0.16; 'subject:user': 0.16; 'input': 0.17; 'cc:addr:python-list': 0.17; 'tue,': 0.17; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'file,': 0.22; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'clause': 0.23; 'url:mailman': 0.26; 'received:209.85.161': 0.26; 'message-id:@mail.gmail.com': 0.28; 'mode': 0.29; '24,': 0.29; "python's": 0.29; 'subject:HELP': 0.29; 'cc:addr:python.org': 0.30; 'url:listinfo': 0.30; "'a')": 0.30; 'skip:\xc2 20': 0.30; '\xc2\xa0\xc2\xa0\xc2\xa0': 0.30; 'print': 0.31; 'break': 0.33; 'chris': 0.34; 'file': 0.34; 'implies': 0.35; 'open': 0.36; 'similar': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.37; 'url:python': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'user': 0.39; 'received:209': 0.39; 'attempt': 0.39; 'current': 0.40; 'help': 0.40; 'addition': 0.60; 'subject:, ': 0.60; 'best': 0.60; 'your': 0.60; 'below': 0.61; 'opened': 0.63; 'dear': 0.63; '8bit%:100': 0.68; 'skip:\xc2 10': 0.72; 'press': 0.72; 'directed': 0.80; 'sender:addr:chris': 0.84; 'url:tutor': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=V8RJ0QJ0J6N38qQXsn7LE9CyesL+kJW5t8ZU47dFJ/0=; b=U5UJMOS/S6hu250QEzAzUm1Co+sqRSKtTLjwn2lJiDLGU+/9yyNt3R5pkFrOXwqrul aAMG4Q0aSaEVD3eS9/3FRnkF1JaJSLb5YxnX7uyfVL9ri/TmnnzWbBVMNmaAAIFOEcOV 0h0DvF8PEzWsSoeXTnTuzCSxde+G496p7SZHU=
DomainKey-Signature a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=AAcGGWjLIR0Wb+k8aOYgpZNt5asfcs828QtJCpKYUQJU4gQvnKJ+kBycL0VEBfY+9o tpRju/99+x9sfRbItA44sZ/EESLDBc5QWcNsuoFWW+pL8UmSxbjTf2tQqf43f7rl9Avk 3ImOYymCcbaJRJC/UVAboT27j54AYOp6O+dBg=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <BANLkTinh6V3-DtdU5davGdP5a53a2eDF_Q@mail.gmail.com>
References <BANLkTinh6V3-DtdU5davGdP5a53a2eDF_Q@mail.gmail.com>
Date Tue, 24 May 2011 01:58:25 -0700
X-Google-Sender-Auth n_MmJFOFPUPFsl9KV5wrR-p6By4
Subject Re: NEED HELP- read file contents, while loop to accept user input, and enter to exit
From Chris Rebert <clp2@rebertia.com>
To Cathy James <nambo4jb@gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2008.1306227882.9059.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 82.94.164.166
X-Trace 1306227882 news.xs4all.nl 49038 [::ffff:82.94.164.166]:39543
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6125

Show key headers only | 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