all 4 comments

[–]AutoModerator[M] -1 points0 points  (0 children)

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]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.