How to complete with Python without tampering with system settings. Python3.5(3.4)~
To enter the password from other than the terminal with sudo, you need to pass it from the standard input with the -S option. A newline character is also required.
Pass "password + newline character" as a byte string to the argument input of subprocess.run () (subprocess.check_output for Python3.4) (argument input was added from Python3.4).
An example of mounting multiple ISO files in sequence and unmounting them.
import os
import subprocess
import getpass
import tempfile
#Creating a temporary mount point
mp = tempfile.mkdtemp()
#Enter password
passwd = (getpass.getpass() + '\n').encode()
while True:
path = input('Input file path (".quit" to quit) : ')
if path == '.quit':
os.rmdir(mp)
raise SystemExit
subprocess.run(('sudo', '-S',
'mount', '-t', 'iso9660', '-o', 'loop', path, mp),
input=passwd, check=True)
"""Gonyo Gonyo"""
subprocess.run(('sudo', '-S', 'umount', mp), input=passwd, check=True)
http://docs.python.jp/3/library/subprocess.html
Recommended Posts