Inserting empty space into an existing file/pointer
Charles G Waldman
cgw at fnal.gov
Tue Jul 6 17:10:17 EDT 1999
It's not really a Python question, it's a matter of what operations
are supported by the underlying filesystem. And there's no filesystem
that I know of that will let you "insert" stuff into a file. (I'm
interested to see if any of the gurus here tell me I'm wrong).
You could however do something like this:
import os
filename = "whatever"
infile = open(filename,'r')
outfile = open(filename+".tmp",'w')
outfile.write(infile.read(initial_count))
outfile.write('\000' * n_zeros)
outfile.write(infile.read(final_count))
outfile.close()
os.rename(filename, filename+".old")
os.rename(filename+".tmp", filename)
Note that this might be somewhat memory-intensive if "initial_count"
or "final_count" are large. Breaking the I/O into blocks is left as
an exercize.
More information about the Python-list
mailing list