[Tutor] How to write lines in a text file (GUI)

Mic o0MB0o at hotmail.se
Tue Nov 29 20:24:24 CET 2011

>Note: I'm struggling to follow your code whilst reading on my phone so the 
>following suggestions make no reference to your particular codebase

Okay I understand. Perhaps it doesn't matter.

>Let's first assume that all buttons could be pressed in any order, 
>therefore given the following presses: but1, but2, but2, but3, we will have 
>the first line with hi_1, the second line empty and the third line hi_3. 
>Now I also assume that there >is no 'update file' button, therefore each 
>press of a button needs to update the file making a change to a single 
>line. That sounds to me like a good use for an update_file function that 
>accepts a parameter of which line should be updated. >So the main challenge 
>is to write a function that can read/write to a file certain data depending 
>on the argument passed. The function should:

>Read file
>Identify line to be changed
>If line empty
>Change line to hi_#
>Write file

>When you open a file, you could then use the read function to read the 
>whole file, or you could use the readlines function to read the file line 
>by line. Experiment with both, see what data types they both return and 
>decide which is most >suitable. Bear in mind you need to easily 
>differentiate each line and access them individually.

>If your still struggling to write this function, show us how far you get 
>and we can go from there.

Actually, I nearly know how do to solve this problem. I just have one 
problem. Can I use writeline() to write text into line 3 or line 5 for 
Say that I want to write the text "hi" into line five of a text file, can I 
do this using writeline(), if so, how ?

Thanks for the response!

Hope this helps,
Sent from my BlackBerry® wireless device

More information about the Tutor mailing list