6

I need to create files of arbitrary size that contain no data. The are potentially quite large. While I could just loop through and write a single null character until I've reached the file size, that seems ugly.

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

What is the efficient, pythonic way to do this?

Daniel Von Fange
  • 5,973
  • 3
  • 26
  • 23

2 Answers2

19

You can seek to a specific position and write a byte, and the OS will magically make the rest of the file appear.

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

You need to write at least one byte for this to work.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • And the filesystem needs to support sparse files – John La Rooy Jul 31 '10 at 11:58
  • 3
    This still works if the filesystem doesn't support sparse files; in that case the OS will create a non-sparse file of the equivalent size. – Greg Hewgill Jul 31 '10 at 12:02
  • Yes of course, I for some reason imagined the question asked for a sparse file :) – John La Rooy Jul 31 '10 at 12:05
  • 1
    Thanks! I didn't realize I could seek past the end of the file. This actually removes my need to write a "blank" the file in first place, since I can just write chunks to it in any order. – Daniel Von Fange Jul 31 '10 at 19:39
  • Small query regarding 2nd argument of open() call. "wb" is the argument, where 'w' for write mode and 'b' for binary mode. Is it mandatory to open the file in Binary mode also ? If not, what is the advantage of opening the file in binary mode ? – Raj Jul 21 '16 at 06:25
8
with open('zero', 'w') as f:
    f.seek(999999999)
    f.write('\0')

Will create a sparse file if the OS supports it. The magic is that files created this way do not take any space (until you copy it elsewhere with a program that does not preserve holes)

Marco Mariani
  • 13,556
  • 6
  • 39
  • 55