I'm not sure, I also don't have access to the python docs atm, but the simplest method would be to read the whole file into a list, edit the correct item (remembering to count from 0 not 1) and then writing the whole list back to the file.
Bodsda ------Original Message------ From: Mic To: [email protected] To: [email protected] To: Tutor - python List Subject: Re: [Tutor] How to write lines in a text file (GUI) Sent: 29 Nov 2011 19:24 >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 example? 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, Bodsda Sent from my BlackBerry® wireless device Sent from my BlackBerry® wireless device _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
