Wednesday 15 July 2015

Append a text line to a file and remove the first one if there are more than 4 lines in python -


I have an IRC client, while I'm chatting while broadcasting my desktop. I wanted to show it on my broadcast and found an easy Shock-Wave Flash Title Plug-in that could read text files and display them on the screen.

My IRC client uses Python scripts and can call the function every time someone writes a message.

I can write these text lines in a text file, like they come, but I want a method of coding in Python, when the text is more than '4' lines. >

I had planned to do this when the append line function is called, after reading the last 3 lines, adding new and rewriting it, writing it in the original file.

Of course when the script runs first, the file should have at least 3 lines so that the dragon must have an account for it and only read the previous 2 or 1 or 0 ...

I tried to write some code in the last question but it is not working so I will not add it here.

you should not give

  1. using a deck Open the input file

    / code> to get to the last few lines, and add your new line
    (using a new line separator \ n )

  2. Reopen file as output file and write your archive lines. Open the import deck path = 'test.txt' from the archive (path, 'r') as in the file: lines = deck (file, 4) ) Lines .append ("\ nIt is an extra line.") Open with file (path, 'W') as file: file.writelines (lines) < / Html>

No comments:

Post a Comment