I’m trying to automate mounting and unmounting of UNC drives with Python on Windows. I’m using the subprocess module to the execute the various commands so I can log their output. However, the NET USE command occasionally prompts the user for input (like a password). Usually things work well, but since I am using subprocess.communicate(), my program hangs indefinitely when NET USE asks my “subprocess” for input. Of course, I have no idea it’s asking for input. It just sits there waiting patiently and making me frustrated.
This is a more general problem than just NET USE, so please don’t go that route. There are other instances where some (other) program asks for input and my program freezes. Any thoughts?
Some programs are smart and realize that if there is no way for a user to input anything, then they shouldn’t even ask. For such programs, you can add “stdin=open(‘/dev/null’)” to the subprocess.Popen options.
In the more difficult case, where the program really insists on interacting, all you can do is provide the interaction yourself.
EDIT: I should mention that the pexpect library is a great way to do such interaction.