How to create a virtual file for output?

3.9k views Asked by At

I need to create a virtual file in memory to trick the system and to let it think that it exists.

I have some scientific program and I heed to provide in some function file for output as argument.

I want to create some file in memory and provide this file to this function.

I do not want to use some temp folders and so on. How do you think, is it possible to create this trick?

1

There are 1 answers

7
Aaron On

The io library can create in-memory file like objects that act like file handles opened with the typical f = open(filename, mode). If you would have opened in normal text mode, a StringIO should do the trick, or if you would have opened the file in binary mode, BytesIO is the way to go.