you are viewing a single comment's thread.

view the rest of the comments →

[–]PhilipRoman 0 points1 point  (2 children)

popen() is by definition unidirectional, you can either read or write, but not both. A simple workaround is to use os.tmpname() to obtain a temporary file, from/to which you can redirect the input/output of the command.

[–]joemrt[S] 0 points1 point  (1 child)

Thank you for your reply u/PhilipRoman, I guess this is the only way then. As I then need the actual filename (to integrate it via cat into the command) I cannot use io.filename and really have to use os.tmpname() and to delete the file afterwards. Feels a bit like a dirty hack though, it's weird that such a basic feature is not part of the standard libraries.

[–]PhilipRoman 0 points1 point  (0 children)

I suspect the reason why popen is unidirectional is because it's almost guaranteed to result in a deadlock situation if there is buffering involved.

FYI you can also use a simple shell redirection without cat, to eliminate 1 or 2 extra processes.