blindguy
blindguy

Reputation: 1009

Cut off end of file without creating a new one

I have a program that spits out a very large text file that is 75% garbage at the end. Id like to be able to just cut the end off the file with out looping through each line and printing to a new file. Is there anyway to do this in python?

its hard to provide an example because it would be a very large file. ill give it a try:

good line good line bad line bad line bad line bad line bad line EOF

I would like to cut off all of the bad lines. Lets assume for the moment that I could seek() to the spot where I need to cut off the file.

Upvotes: 0

Views: 394

Answers (1)

Lav
Lav

Reputation: 2274

Check truncate() method of file objects, it truncates the file from current position (or to a specified size, but your request implies you're actually reading the file when the decision to delete it's tail is made).

You can check the method documentation here:

https://docs.python.org/2.7/library/stdtypes.html#file-objects

Upvotes: 1

Related Questions