如何在Python中创建虚拟磁盘? [英] How can I create a ramdisk in Python?

查看:639
本文介绍了如何在Python中创建虚拟磁盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Python创建一个虚拟磁盘。我希望能够以跨平台的方式执行此操作,因此它可以在Windows XP至7,Mac和Linux上运行。我希望能够像普通驱动器一样对ramdisk进行读写,最好是带有驱动器号/路径。

I want to create a ramdisk in Python. I want to be able to do this in a cross-platform way, so it'll work on Windows XP-to-7, Mac, and Linux. I want to be able to read/write to the ramdisk like it's a normal drive, preferably with a drive letter/path.

我想要这样做的原因是编写测试用于创建具有特定结构的目录的脚本。我想在ramdisk中完全创建目录,以便确保测试结束后将其完全删除。我考虑过使用Python的 tempfile ,但是如果测试将在中间停止,则该目录可能不会被删除。我想完全确定即使在测试过程中有人拔掉了计算机上的插头,它也会被删除。

The reason I want this is to write tests for a script that creates a directory with a certain structure. I want to create the directory completely in the ramdisk so I'll be sure it would be completely deleted after the tests are over. I considered using Python's tempfile, but if the test will be stopped in the middle the directory might not be deleted. I want to be completely sure it's deleted even if someone pulls the plug on the computer in the middle of a test.

推荐答案

如何关于 PyFilesystem

https:/ /docs.pyfilesystem.org/en/latest/reference/memoryfs.html

https://docs.pyfilesystem.org/en/latest/reference/tempfs.html

缺点是您必须使用PyFilesystem API访问文件系统,但也可以使用PyFilesystem访问实际的文件系统。

The downside is that you have to access the filesystem with PyFilesystem API, but you can also access the real fs with PyFilesystem.

这篇关于如何在Python中创建虚拟磁盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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