Creating file of size x
Andrew Dalke
dalke at dalkescientific.com
Mon Jun 6 19:45:42 EDT 2005
Jan Danielsson wrote:
> Is there any way to create a file with a specified size?
Besides the simple
def make_empty_file(filename, size):
f = open(filename, "wb")
f.write("\0" * size)
f.close()
?
If the file is large, try (after testing and fixing any
bugs):
def make_empty_file(filename, size, block = 32*1024):
f = open(filename, "wb")
written = 0
s = "\0" * block
for i in range(size//block):
f.write(s)
remainder = size%block
f.write(s[:remainder])
f.close()
As Grant Edwards pointed out, you can do a seek(size-1)
but I don't know if it's fully portable.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list