I want to execute a mysqldump in python and provide the password when it is requested from the mysqldump.
Adding the password in the command line is not an option, it must be provided via stdin.
This is what I've done so far:
command = [
mysqlfile = mysqlpath + "/" + mysqldb + ".sql"
with open(mysqlfile, "w+") as file:
p = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=file)
You can use pexpect for that. This is modified code as I had to test it, but you get the idea:
import pexpect command2 = 'mysqldump -h localhost -u root -p xyzzy' mysqlfile = "/tmp/foo.sql" with open(mysqlfile, "w+") as file: p = pexpect.spawn(command2) p.expect("Enter password: ") p.sendline("foobar") q = p.read() p.wait() file.write(q)
here "foobar" is my database password.