Search code examples
pythonfileioram

Python raspberry pi - how to treat RAM memory as a file


on raspberry pi opencv python, I'm successfully capturing an image from a USB cam, then saving as 'image.png' then passing the filename to an smtp client that reads in the file and sends it as an attachement in an email. obviously this is bad for the sd card...

how do I treat an area of RAM as a file? that I can stream data to and from?..

I'm not at home now but if needed I can attach code of what I've tried so far.

cheers


Solution

  • You can create a ram drive using the following;

    sudo mount ramfs <DIRECTORY> -t ramfs -o size=<SIZE>    
    

    An example that creates a 16 megabyte ram drive at /ram:

    sudo mount ramfs /ram -t ramfs -o size=16M
    

    If this is working for you, and you want to make sure this is automatically created every time you start your pi, you'll need to update your /etc/fstab.


    Once you've done this, you won't need to make any architectural changes to your code. Just make sure your file operations are happening in this directory. Needless to say, anything you store here will magically disappear if you restart your pi for any reason.