Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a ramdisk in Python?

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.

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.

like image 603
Ram Rachum Avatar asked Dec 04 '10 00:12

Ram Rachum


People also ask

What is RAM disk file system?

A ramdisk is a volatile storage space defined in the RAM memory. Using this feature increases file processing performance ten times over the best SSD hard disks. Implementing a ramdisk is very advantageous for users whose tasks require significant amounts of hardware resources.

Is TMP a RAM disk?

This tutorial will show you how to turn your /tmp directory into a "RAM disk". Essentially this means that any file stored in /tmp is actually being stored in memory rather than on your persistent storage devices.

What is Tmpfs in Linux?

A temporary file system (TMPFS) uses local memory for file system reads and writes, which is typically much faster than reads and writes in a UFS file system. TMPFS file systems can improve system performance by saving the cost of reading and writing temporary files to a local disk or across the network.


1 Answers

How about PyFilesystem?

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

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

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

like image 64
Epeli Avatar answered Sep 28 '22 05:09

Epeli