How to truncate x bytes of the head file ? I have a log which has 5 GB and I want to cut first 3 GB ( remove old information) .
Asked
Active
Viewed 292 times
4
-
possible duplicate of [How to delete parts of a file in python?](http://stackoverflow.com/questions/4692065/how-to-delete-parts-of-a-file-in-python) – Mikael Ohlson Apr 29 '15 at 08:34
1 Answers
4
Use the seek
method:
fname = 'bigfile.log'
fid = open(fname, "rb")
fid.seek(3 * (2 ** 30) , 0) # go to the ~(3*10^9)th Byte, with respect to the start
Buffer = fid.read(2 * (2 ** 30))
Click here for more information.

dobkind
- 416
- 4
- 10
-
-
I think it is not efficient. The intention was to demonstrate how to access a specific byte in the file. If it was up to me , I would read the file using a loop. – dobkind Apr 29 '15 at 08:59