如何在Windows 7中使用Python廉价地创建一个非常大的文件? [英] How to create a very large file cheaply using Python in Windows 7?

查看:153
本文介绍了如何在Windows 7中使用Python廉价地创建一个非常大的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:
在Windows系统上快速创建大文件?


出于测试目的,我希望能够创建和删除非常大的文件(几个GB)。我不需要写任何特定的东西。他们可能是随机数据。有没有办法通过简单地分配磁盘空间来生成一个大文件?如果不是,写这样的文件最快的方法是什么?要在几秒钟内创建文件是可取的。



我需要在Python脚本中执行此操作,并在Windows 7中执行此操作。

解决方案

这应该在ntfs文件系统上工作,因为它们支持稀疏文件

pre $ 以open(file.to.create,w)作为文件:
file.truncate(10 ** 10)

该文件会显示为\ x00字节,但是这些实际上只是在您从文件中读取的时候根据需要创建的。它使用几乎没有磁盘空间(尽管它可能看起来像它从一开始就使用所有的10 GB)我发现没有简单的方法来检查窗口中的真实文件大小),并增长在写入时分配所需的块。 AFAIK很可能创建一个比它驻留的磁盘大得多的稀疏文件,尽管这可能会在以后引起麻烦。 :)注意:如果你复制一个稀疏文件,它可能会在这个过程中展开成一个非稀疏文件(读取fake\x00 bytes,写入 real \x00 bytes)。这是它看起来像一个普通的10 GB的空字节文件,以便向后兼容的结果 - 必须执行单独的检查以将其显示为稀疏文件。要成功复制稀疏文件并将其保存为稀疏文件,必须满足以下两个条件:


  • 用于复制的工具必须是知道稀疏文件,而
  • 它复制到的文件系统必须支持稀疏文件



例如,默认情况下,USB拇指驱动器/钢笔通常使用旧的FAT文件系统进行格式化,而不是支持稀疏文件。从测试中,Windows XP的资源管理器在复制时不会保留稀疏文件。 此提示建议 Robocopy 可以胜任这项工作,但是我没有测试过。


Possible Duplicate:
Quickly create large file on a windows system?

For testing purposes, I would like to be able to create and delete very large files (several GB's). I have no need to write anything specific to them. They could be random data. Is there a way to generate a large file by simply allocating the space on disk? If not, what is the quickest way to write such a file? To create the file in a matter of a few seconds is desirable.

I need to do this within a Python script and am doing so on Windows 7.

解决方案

This should work on ntfs filesystems, since they support sparse files. It's almost instantaneous.

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

The file will appearantly be filled with \x00 bytes, but those are in fact just created as needed when you read from the file. It uses almost no disk space (though it may look like it uses all 10 GB from the very beginning -- I've found no easy way to check the real file size in windows), and grows by allocating needed blocks when you write to it. AFAIK it's quite possible to create a sparse file that is much larger than the disk it resides on, though this may of course lead to trouble later on. :)

Beware: if you copy a sparse file, it may in the process expand to a non-sparse file (read "fake" \x00 bytes, write real \x00 bytes). This is a consequence of the fact that it looks just like an ordinary 10 GB file with null bytes in order to be "backward compatible" -- separate checks must be performed to reveal it as a sparse file. To successfully copy a sparse file and keep it a sparse file, two conditions must be met:

  • the tool used to copy it must be "aware" of sparse files, and
  • the filesystem it is copied to must support sparse files

For example, USB thumbdrives/pens are typically formatted with the old FAT filesystem by default, and it does not support sparse files. From testing, explorer of Windows XP appears not preserve sparse files when copying. This tip suggests Robocopy is up to the job, but I've not tested it.

这篇关于如何在Windows 7中使用Python廉价地创建一个非常大的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆