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.
source share