Bug writing/reading to file.

Discussion in 'Python' started by arne.k.h@gmail.com, Dec 24, 2007.

  1. Guest

    Hi! :)

    Im new to python, and I have made a electronic diary - its just a
    task. Here is the code:
    http://pastebin.com/m49391798

    The bug is (feel free to download and test it) that i can't see what i
    wrote in the diary without restarting the program. Here is an example:

    1: I start the program
    2: (text.txt is empty)
    3: I write "hello, how are you?" with the writing-function in the
    program
    4; I use the read-all-function in the program.
    5: There is no entries in the diary. But if I restart the program, I
    can read the file.

    The strange thing is that if i watch the file contents while im using
    the program, I see that i add the line "hello, how are you?" to the
    file, but i can't read it with the read-function.

    Can this be a problem with file-pointers? And how can i solve it?
     
    , Dec 24, 2007
    #1
    1. Advertising

  2. text.txt is only read when you open the file, that means that your read
    statement, doesnt now of what has been written to the file, since it was
    opened..

    under if if option == "1":
    you should add, then it will be re-read for every read
    abc = open('text.txt')
    query = abc.readlines()

    man, 24.12.2007 kl. 03.53 -0800, skrev :
    > Hi! :)
    >
    > Im new to python, and I have made a electronic diary - its just a
    > task. Here is the code:
    > http://pastebin.com/m49391798
    >
    > The bug is (feel free to download and test it) that i can't see what i
    > wrote in the diary without restarting the program. Here is an example:
    >
    > 1: I start the program
    > 2: (text.txt is empty)
    > 3: I write "hello, how are you?" with the writing-function in the
    > program
    > 4; I use the read-all-function in the program.
    > 5: There is no entries in the diary. But if I restart the program, I
    > can read the file.
    >
    > The strange thing is that if i watch the file contents while im using
    > the program, I see that i add the line "hello, how are you?" to the
    > file, but i can't read it with the read-function.
    >
    > Can this be a problem with file-pointers? And how can i solve it?

    --
    Lars Johansen <>
    eZ Systems
     
    Lars Johansen, Dec 24, 2007
    #2
    1. Advertising

  3. Arne Guest

    On Dec 24, 12:53 pm, wrote:
    > Hi! :)
    >
    > Im new to python, and I have made a electronic diary - its just a
    > task. Here is the code:http://pastebin.com/m49391798
    >
    > The bug is (feel free to download and test it) that i can't see what i
    > wrote in the diary without restarting the program. Here is an example:
    >
    > 1: I start the program
    > 2: (text.txt is empty)
    > 3: I write "hello, how are you?" with the writing-function in the
    > program
    > 4; I use the read-all-function in the program.
    > 5: There is no entries in the diary. But if I restart the program, I
    > can read the file.
    >
    > The strange thing is that if i watch the file contents while im using
    > the program, I see that i add the line "hello, how are you?" to the
    > file, but i can't read it with the read-function.
    >
    > Can this be a problem with file-pointers? And how can i solve it?


    The problem is solved! Thanks for the help!
     
    Arne, Dec 24, 2007
    #3
  4. 2007/12/24, <>:
    > Hi! :)
    >
    > Im new to python, and I have made a electronic diary - its just a
    > task. Here is the code:
    > http://pastebin.com/m49391798
    >
    > The bug is (feel free to download and test it) that i can't see what i
    > wrote in the diary without restarting the program. Here is an example:
    >
    > 1: I start the program
    > 2: (text.txt is empty)
    > 3: I write "hello, how are you?" with the writing-function in the
    > program
    > 4; I use the read-all-function in the program.
    > 5: There is no entries in the diary. But if I restart the program, I
    > can read the file.
    >
    > The strange thing is that if i watch the file contents while im using
    > the program, I see that i add the line "hello, how are you?" to the
    > file, but i can't read it with the read-function.
    >
    > Can this be a problem with file-pointers? And how can i solve it?


    When you start you store 'text.txt' lines in query, and since it was
    empty there were no lines so you are storing an empty list. Then later
    you ask to print all the entries iterating this empty list and it
    correctly prints nothing. To solve this you need to reread 'text.txt'
    lines.

    Besides the solution I need to make some points:
    - Post a shorter sample next time
    - You don't need to use readlines() for this, just use for line in
    yourfileobject: ...
    - Also, you dont need that x, you could use for indx, line in
    enumerate(yourfileobject): ...
    - I didn't look at the rest of the code, so there could be more
    corrections worth being noted

    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    --
    -- Guilherme H. Polo Goncalves
     
    Guilherme Polo, Dec 24, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Hearn
    Replies:
    1
    Views:
    327
    avnrao
    Jun 30, 2004
  2. HNguyen
    Replies:
    4
    Views:
    2,453
    HNguyen
    Dec 21, 2004
  3. Replies:
    8
    Views:
    1,137
    Andrew Thompson
    Dec 17, 2005
  4. john smith

    mix mode file reading/writing?

    john smith, Jul 29, 2003, in forum: C++
    Replies:
    4
    Views:
    382
    Thomas Matthews
    Jul 30, 2003
  5. Giulio
    Replies:
    0
    Views:
    384
    Giulio
    Sep 22, 2003
Loading...

Share This Page