FAQ
dear mentor,

I need help with my code:
1) my program won't display file contents upon opening
2) my program is not writing to file
3) my program is not closing when user presses enter- gow do I do this with
a while loop?

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():
f.write(s)
break
else:
f = open ('c:/testing.txt', 'r')
f.readlines()
for line in f:
print (line)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110524/6a9d9e99/attachment.html>

Search Discussions

  • Tim Golden at May 24, 2011 at 8:46 am

    On 24/05/2011 09:31, Cathy James wrote:
    dear mentor,
    I need help with my code:
    1) my program won't display file contents upon opening
    #1) open file and display current file contents:
    f = open ('c:/testing.txt'', 'r')
    f.readlines()
    If you're running this in an interactive interpreter, I would
    expect it to show a list of lines (assuming c:/testing.txt has
    something in it...). If you're running it as a program, though,
    it won't show anything: you need to actually output the result
    of the expression f.readlines (). It only happens at the
    interpreter as a development convenience:

    f = open ("c:/testing.txt", "r")
    print f.readlines ()
    # or print (f.readlines ()) if you're in Python 3

    TJG
  • Chris Angelico at May 24, 2011 at 8:46 am

    On Tue, May 24, 2011 at 6:31 PM, Cathy James wrote:
    ??? s = input ('enter name: ').strip()
    Are you using Python 2 or Python 3? If it's Python 2, this should be
    raw_input().
    ??? f = open ('c:/testing.txt', 'a')
    ...
    ??????? f = open ('c:/testing.txt', 'r')
    You may be having trouble here as a result of not closing the file and
    then trying to reopen it.

    Also, at some point you have to check if 's' (the user's inputted
    string) is empty. You can then leave the loop using the 'break'
    statement.

    Hope that helps! Best of luck with your homework.

    Chris Angelico
  • Chris Rebert at May 24, 2011 at 8:58 am

    On Tue, May 24, 2011 at 1:31 AM, Cathy James 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
  • Jean-Michel Pichavant at May 24, 2011 at 9:22 am

    Cathy James wrote:
    f = open ('c:/testing.txt'', 'r')
    replace the double quote by a single quote.

    JM

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 24, '11 at 8:31a
activeMay 24, '11 at 9:22a
posts5
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase