Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Bug writing/reading to file.

Reply
Thread Tools

Bug writing/reading to file.

 
 
arne.k.h@gmail.com
Guest
Posts: n/a
 
      12-24-2007
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?
 
Reply With Quote
 
 
 
 
Lars Johansen
Guest
Posts: n/a
 
      12-24-2007
´╗┐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 http://www.velocityreviews.com/forums/(E-Mail Removed):
> 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 <(E-Mail Removed)>
eZ Systems

 
Reply With Quote
 
 
 
 
Arne
Guest
Posts: n/a
 
      12-24-2007
On Dec 24, 12:53*pm, (E-Mail Removed) 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!
 
Reply With Quote
 
Guilherme Polo
Guest
Posts: n/a
 
      12-24-2007
2007/12/24, (E-Mail Removed) <(E-Mail Removed)>:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
ASP.NET Login control bug or SQL 2005 bug? RedEye ASP .Net 2 12-13-2005 10:57 AM
Re: BUG? OR NOT A BUG? John ASP .Net 2 09-21-2005 10:31 AM
Bug Parade Bug 4953793 Michel Joly de Lotbiniere Java 4 12-02-2003 05:05 AM
how to report bug to g++ ? got a bug and fixed up source code DarkSpy C++ 4 06-27-2003 09:05 AM



Advertisments