51

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.

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
  • 7
    I rather suspect there's no simple, cross-platform way to do this, as each OS handles mounting differently. Very interesting question, though! – Ben Blank Dec 04 '10 at 00:12
  • "if the test will be stopped in the middle the directory might not be deleted" Do you have any evidence of this? That contradicts the documentation. – S.Lott Dec 04 '10 at 00:18
  • 1
    @S.Lott I imagine if the test doesn't (get a chance to) clean up after itself. In which case I'd argue: Why not simply make the test setup ensure a clean state (delete directory if required, perhaps), regardless of existing state? –  Dec 04 '10 at 00:30
  • @pst: The file is deleted when it's closed. There's no "cleanup" in the test -- the delete is part of the OS definition of the file. Nothing to do with any application software. – S.Lott Dec 04 '10 at 02:53
  • 2
    @S.Lott: "The user of `mkdtemp()` is responsible for deleting the temporary directory and its contents when done with it." From Python's `tempfile` docs. – Ram Rachum Dec 04 '10 at 11:19
  • @pst: I want the computer to remain clean of junk directories/files between test runs as well. – Ram Rachum Dec 04 '10 at 11:20

4 Answers4

22

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.

Rob Kwasowski
  • 2,690
  • 3
  • 13
  • 32
esamatti
  • 18,293
  • 11
  • 75
  • 82
  • 2
    This currently seems like the best solution, but I *will* need substitutes for `os.mkdir` (and other such functions) that operate on `MemoryFS`. If no such functions exist, I'll have to write my own. – Ram Rachum Dec 04 '10 at 14:34
  • 1
    Actually it seems that you can mount the MemoryFS with fuse on Linux systems and access that with stock functions of Python. http://www.willmcgugan.com/2010/6/20/pyfilesystem-03-released/ http://packages.python.org/fs/expose/fuse.html#module-fs.expose.fuse – esamatti Dec 04 '10 at 14:46
  • The Fuse thing is really awesome, but the fact it isn't cross-platform is a problem. Also, some of it is GPL licensed which can be a problem. – Ram Rachum Dec 04 '10 at 16:33
  • 1
    The link to memoryfs has rotted. Here is the current link: https://docs.pyfilesystem.org/en/latest/reference/memoryfs.html – philologon Jan 27 '19 at 02:37
  • This doesn't actually work now. The fs.expose module with fuse doesn't even exist. – user3496060 Oct 17 '21 at 21:42
4

Because file and directory-handling is so low-level and OS dependent, I doubt anything like what you want exists (or is even possible). Your best bet might be to implement a "virtual" file-system-like set of functions, classes, and methods that keep track of the files and directory-hierarchy created and their content.

Callables in such an emulation would need to have the same signature and return the same value(s) as their counterparts in the various Python standard built-ins and modules your application uses.

I suspect this might not be as much work as it sounds -- emulating the standard Python file-system interface -- depending on how much of it you're actually using since you wouldn't necessarily have to imitate all of it. Also, if written in Pure Python™, it would also be portable and easy to maintain and enhance.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 2
    there are python modules such as [fs](http://pypi.python.org/pypi/fs/0.3.0) that do nearly this but because it's hard to wrap os they provide their own interface which doesn't match but has all of the operations as methods on a object – Dan D. Dec 04 '10 at 01:47
1

One option might be to inject (monkey patch) modified versions of the methods used in the os module as well as the builtins open and file that write to StringIO files instead of to disk. Obviously this substitution should only occur for the module being tested;

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
-7

Please read this:

http://docs.python.org/library/tempfile.html#tempfile.TemporaryFile

"Return a file-like object that can be used as a temporary storage area. The file is created using mkstemp(). It will be destroyed as soon as it is closed (including an implicit close when the object is garbage collected)."

It's all handled for you. Do nothing and it already works.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 2
    As I said in the question, I need to create directories, not just files. From your link: "The user of `mkdtemp()` is responsible for deleting the temporary directory and its contents when done with it." – Ram Rachum Dec 04 '10 at 13:46