Hristo Asenov
Hristo Asenov

Reputation: 449

Is there an alternative way to close stdin file descriptor (currently using <&- with shell=True)?

I would like to write a python script that interacts with dmenu. I was able to figure out that if I want dmenu to take user input only, I can pass

dmenu <&-

and it will display a clean user input prompt. I know the equivalent way of doing that in python using subprocess is

subprocess.check_output("dmenu <&-", shell=True)

However I don't think this is the best way, since if I am using a prompt, it will interpret the prompt text as bash syntax (for instance in "dmenu -p 'Project?'" it will interpret the '?').

Is there a way to use subprocess.Popen to achieve the same? I tried a whole bunch of different stuff such as

subprocess.Popen("dmenu", stdin=None)
import sys
sys.stdin.close()
subprocess.Popen("dmenu", stdin=sys.stdin)

Is using shell=True the only way to achieve this?

Upvotes: 0

Views: 243

Answers (1)

Ry-
Ry-

Reputation: 225144

All you need to do is provide no input to dmenu, so:

subprocess.check_output("dmenu", input=b"")

Upvotes: 2

Related Questions