Skip to content
Advertisement

How do I create a file I can stream data to in Python?

I’d like to create a file similar to those under /dev that I can stream lines of text to without actually writing anything to the disk. I want to still be able to read this stream like a regular text file.

Advertisement

Answer

Call the os.mkfifo function, then open the file it creates as normal. Anything that gets written by one process will get read back out by another, and not saved to disk or anywhere else along the way. Note that reads and writes will block (i.e. appear to hang) if one process gets too far ahead of the other.

Alternatively, you can use the socket library to create a UNIX domain socket, which is bidirectional and has more features, but is more complicated to set up.

Advertisement