I am writing a bit torrent client and allocate space on hard drive for each file I have to download.
File can be huge and I want to overwrite some piece of it without deleting all content.
I read some other answers, but they suggest creating a temporary space and then copying changes back. That would be too heavy process.
I am sure there must be some utility.
Since you are talking about a download file, you should consider the storage of downloaded data in a temporary file(chunks). Refer : streaming & text-file
If I read you question (without python), I would say you could use OS specific commands to overwrite parts of a file.
For example in Linux, use sed
or awk
. Refer : Inserting-a-line-in-a-file-at-a-specific-location
I found a solution, using OS (Linux) specific system call:
fd = os.open(path, os.O_WRONLY)
os.lseek(fd, offset, os.SEEK_SET)
os.write(fd, piece)
os.close(fd)
This did overwrite file without nullifying it. But when had:
with open(path, 'wb') as file:
file.seek(offset)
file.write(piece)
It was deleting all other content.
Program is not portable this way but I don't need it to be.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.