Monday 15 August 2011

list - Copy the last three lines of a text file in python? -


I'm new to the dragon and the way it Handles the variable and the array of variables in the lists is quite strange for me. I usually read a text file in a vector and then by determining the shape of the vector by making a copy of the new array / vector in the last three, For a final function with loop, copy the copy function to a new array of all three.

I do not know how loop works in Pathathan, so I can not do this.

I still have:

  #read line file number OpLex Inket = 3 text_file = Open ("output.txt", "R") lines = text_file. Readlines () text_file.close () write linens = [] if lane (lines)> gt; NumberOfLinesInchat: i = 0 while ((numberOfLinesInChat-i) gt =): written line [i] = lines [(lane (line) - (numberOfLinesInChat-i)] i + = 1 # writes what people are reading The file is called text_file = open ("output.txt", "w") text_file.write (writeLines) text_file.close ()    

To efficiently obtain the last three lines of a file, import decks with import from deck :

  Use ('somefile') as wings: last 3 = deck (wings, 3)   

I remember so that you really do not want to.

Note - Your full code will be:

  Open the import deck from the archive ('some files') in the form of wings, open ('outputfile', 'w') As fout: fout.writelines (deque) (wings, 3))    

No comments:

Post a Comment