I want to copy a file without the first 256 bytes.
Is there a nice way to do it in python?
I guessing that the simple way is to read byte-byte with a counter and then start copy only when it get to 256.
I was hoping for more elegant way.
Thanks.
To delete the first few bytes, you can either overwrite them, or you have to rewrite and re-align the whole file. Something like dd can do this fairly efficiently.
Using the truncate Command. The command truncate contracts or expands a file to a given size. The truncate command with option -s -1 reduces the size of the file by one by removing the last character s from the end of the file. The command truncate takes very little time, even for processing large files.
dd is a command-line utility for Unix and Unix-like operating systems whose primary purpose is to convert and copy files. On Unix, device drivers for hardware (such as hard disk drives) and special device files (such as /dev/zero and /dev/random) appear in the file system just like normal files.
with open('input', 'rb') as in_file:
with open('output', 'wb') as out_file:
out_file.write(in_file.read()[256:])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With