Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete the first line of a text file?

Tags:

python

file

I have been searching online, but have not found any good solution.

Here is my text file:

[54, 95, 45, -97, -51, 84, 0, 32, -55, 14, 50, 54, 68, -3, 57, 88, -1] [24, 28, 38, 37, 9, 44, -14, 84, -40, -92, 86, 94, 95, -62, 12, -36, -12] [-26, -67, -89, -7, 12, -20, 76, 88, -15, 38, -89, -65, -53, -84, 31, -81, -91] [-19, -50, 16, 47, -42, -31, 75, 0, 25, -95, 75, 97, 19, 77, -2, -31, -59] [-66, -10, 35, -39, 24, 70, 74, -45, -27, 77, -44, 86, 57, 14, -91, -26, -20] [-63, 80, -31, 70, 100, 22, -30, 74, 44, -35, -25, -75, -39, -13, -93, 0, 1] [63, 13, 67, 55, -56, 45, 10, 61, -14, -55, 40, 84, -59, 7, 75, -64, -25] [7, -50, -17, -86, -43, 34, 82, 84, 49, 18, 56, -31, -19, 59, -96, 72, -40] [-73, 34, -68, 20, 30, 1, 49, 77, -94, 2, -83, 40, 2, 20, 66, 60, -36] [-80, -12, 93, 77, 73, -55, 24, 3, -60, 12, -41, -43, -49, 36, 6, -93, -24] [-41, 12, -43, 42, -70, 75, -84, -83, 30, 78, -3, 51, 69, 0, 65, 60, -15] [82, 97, -57, -96, 25, -100, 61, 13, -80, -32, 99, 60, 58, -58, -45, -58, -53] [-90, -34, 80, 95, -12, -34, 71, -83, 46, 10, -78, -40, 65, 53, -81, 40, -59] [-80, -20, -87, -2, -54, 74, -79, 22, -20, 60, -84, -12, -40, -98, -81, -5, -35] [33, 36, -46, 10, -77, 88, -99, -5, 19, -20, 89, 87, -47, 46, 10, 17, -67] [-77, 73, 20, 44, 79, -14, -8, -49, 45, -49, -91, -21, 41, -13, 74, -71, -15] [98, -99, 51, 53, 56, -78, 31, 45, 35, -36, -10, -86, 9, 94, 24, -2, -20] [-37, 46, -77, -92, 48, -34, 75, 19, -74, -13, -100, 33, -46, 19, -60, 5, 5] [-13, -30, -82, -70, 64, 87, 16, 67, -36, 22, -99, -92, 36, 8, 90, 48, -5] [46, 75, -15, 24, 24, -37, -3, -45, 32, -84, -2, -16, 43, -88, 92, 27, -10] 

All I want is to delete the first line (which means using the second line as the first line, not filling first line with whitespace).

Could anyone please help me with that?

like image 535
zaolian Avatar asked Dec 04 '13 00:12

zaolian


People also ask

How do I remove the first line from a text file in Java?

Scanner fileScanner = new Scanner(myFile); fileScanner. nextLine(); This will return the first line of text from the file and discard it because you don't store it anywhere.

How do I remove a line from a file?

Deleting line using sed To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.

How do I show the first line of a text file?

To look at the first few lines of a file, type head filename, where filename is the name of the file you want to look at, and then press <Enter>.


1 Answers

Assuming you have enough memory to hold everything in memory:

with open('file.txt', 'r') as fin:     data = fin.read().splitlines(True) with open('file.txt', 'w') as fout:     fout.writelines(data[1:]) 

We could get fancier, opening the file, reading and then seeking back to the beginning eliminating the second open, but really, this is probably good enough.

like image 88
mgilson Avatar answered Sep 24 '22 14:09

mgilson