How to create a very large file cheaply using Python in Windows 7?

Possible duplicate:
Quickly create a large file on a Windows system?

For testing purposes, I would like to be able to create and delete very large files (several GB). I do not need to write anything specific. They may be random data. Is there a way to create a large file by simply allocating disk space? If not, what is the fastest way to write such a file? It is advisable to create a file in seconds.

I need to do this in a Python script, and I do it in Windows 7.

+6
source share
1 answer

This should work with ntfs file systems as they support sparse files . It is almost instant.

with open("file.to.create", "w") as file: file.truncate(10 ** 10) 

The file will be filled with \ x00 bytes, but in fact they are simply created as needed when you read from the file. It almost does not use disk space (although it may seem that it uses all 10 GB from the very beginning - I did not find an easy way to check the real file size in windows) and grows, highlighting the necessary blocks when you write This. AFAIK, it’s quite possible to create a sparse file, which is much larger than the disk on which it is located, although this, of course, can lead to problems later. :)

Beware: if you copy a sparse file, it can expand to an unsharp file in the process (read "fake" \ x00 bytes, write real \ x00 bytes). This is due to the fact that it looks just like a regular 10 gigabyte file with zero bytes in order to be "backward compatible" - separate checks must be performed to identify it as a sparse file. To successfully copy a sparse file and save it in a sparse file, two conditions must be met:

  • the tool used to copy it must be aware of sparse files and
  • the file system to which it is copied must support sparse files

For example, USB sticks / pens are usually formatted with the old FAT file system by default and do not support sparse files. From testing, Windows XP Explorer appears to not save sparse files when copying. This tip suggests Robocopy complete the task, but I have not tested it.

+19
source

Source: https://habr.com/ru/post/891489/


All Articles