file io - python overwrite bytes from offset1 to offset2 -
i've seen multiple tutorials in python, played around bit python file write(), tell(), seek() functions , os.write(), lseek() functions.
but still don't how can following: have: in file know start_offset , end_offset bytes. , need replace bytes start_offset end_offset different set of bytes. how do this??
ftell() returns me start_offset , regex + ftell() returns me end offset have bytes overwrite original ones in file.
but write() takes string write. how overwrite start_pos end_pos??
appreciate pointers/suggestions
despite shouted denial, can use mmap
here.
if @ mmap
constructor in docs, takes parameters offset
, length
. on platforms, both have multiple of pagesize
or similar value, that's not hard.
so:
try: pagesize = mmap.pagesize except nameerror: pagesize = mmap.allocation_granularity def overwrite(fileobj, start, end, newbytes): startoffset, startremainder = divmod(start, pagesize) offset = startoffset * pagesize endoffset, endremainder = divmod(end, pagesize) length = (endoffset + 1) * pagesize - offset map = mmap.mmap(fileobj.fileno(), offset=offset, length=length, access=mmap.access_write) map[startremainder:startremainder+end-start] = newbytes
this has nice advantage if len(newbytes) != end - start
you'll nice exception mmap
, instead of overwriting more or less of file intended , leaving things corrupted.
but it's simpler use seek
, in martijn pieters's answer. here's same function seek
:
def overwrite(fileobj, start, end, newbytes): if len(newbytes) != end - start: raise valueerror('overwrite cannot expand or contract file') fileobj.seek(start) fileobj.write(newbytes)
still, it's worth knowing mmap
can don't dismiss in useful cases in future.
(also, versions of python, on platforms, can have files big seek
in. example, linux /proc/*/map
sparse file size of 1<<64
, on distros, python can't find fseeko
, therefore can't seek farther 1<<63
. so, knowing alternative ways this—os.lseek
, mmap
, etc.—may work around problem 1 day.)
Comments
Post a Comment