-1

I have some files like this:

a:...
a:...
a:...
a:...
count = ...

and I just need to keep the a:... values lines. What is the fastest way to remove that last line (which always starts with count =) ?

philippe lhardy
  • 2,982
  • 26
  • 35
Angel
  • 61
  • 1
  • 1
  • 12
  • What do you want to do with the values? Get them into some data structure, modify the file, or write them to some other stream? – Chris Martin Nov 02 '14 at 08:18

2 Answers2

1

You can try this solution:

lines = file.readlines()
lines = lines[:-1]

After opening your file in python as reading mode you could do that for deleting your last line.

MLSC
  • 5,584
  • 7
  • 49
  • 85
  • That is, after you open your file like `file=open('/dir/to/file/file.txt','r')` or `with open('dir/to/file/file.txt','r') as file:` – Luke Feb 02 '16 at 08:05
1

You could use readlines() function and apply the slice like

 lines = yourfile.readlines()
lines = lines[:-1]

Many other methods could be found here

Community
  • 1
  • 1
Avinash Babu
  • 5,961
  • 3
  • 20
  • 25