I just learned subprocess.check_call() function today. I intend to use it to replace my os.system command.
Originally, I have my command as such:
os.system("mount -t cifs //source/share /mnt/share -o username=user")
The command above will prompt for password and it will mount the drive if the password is correct.
However, if I tried:
cmd_string="mount -t cifs //source/share /mnt/share -o username=user"
subprocess.check_call(cmd_string.split(" "), shell=True, stdin=sys.stdin)
It would not ask for password. Instead, it just print the partitions. It’s like invoke “mount” command without parameters.
Any idea how I can use check_call and still receive interactive input from user?
You are invoking
mountwithout parameters. If you specifyshell=True,subprocessexpects the entire command including arguments is passed to the shell as one big string. If you specifyshell=False(the default if not specified), the command and its arguments are passed as a list of strings, as you have done by usingsplit. By mixing the two forms, you are effectively only passing the stringmountas the command to be executed. Either removeshell=Trueor remove the.split(" "). The first choice is usually better unless there is some reason you need shell parsing to be involved, normally something to be avoided.