How to efficiently fill a file with null data from python?

I need to create files of arbitrary size that do not contain data. They are potentially quite large. Although I could just skip the loop and write one null character until I reached a file size that seems ugly.

with open(filename,'wb') as f:
   # what goes here?

What is an effective, pythonic way to do this?

+5
source share
2 answers

You can search for a specific position and write bytes, and the OS will magically do the rest of the file.

with open(filename, "wb") as f:
    f.seek(999999)
    f.write("\0")

For this you need to write at least one byte.

+16
source
with open('zero', 'w') as f:
    f.seek(999999999)
    f.write('\0')

, . , , , ( , )

+8

All Articles