I'm new to Python, and haven't used Linux in years, so I'm not sure where I'm getting tangled up. I'm trying to use Popen to run sql files in MySQL on Ubuntu.
Here is the relevant code:
command = ['mysql', '-uUSER', '-pPWD','-h192.168.1.132', '--database=dbName', '<', './1477597236_foo.sql' ]
print("command is: "+subprocess.list2cmdline(command))
proc = subprocess.Popen(
command, stderr=subprocess.PIPE, stdout=subprocess.PIPE, cwd='.'
'-e select * from foo'
When a redirection or pipe or built-in command is present in the command line,
shell=True is required. However, in simple cases like this,
shell=True is overkill. There's a much cleaner way in order to avoid
shell=True which gives better control on the input file.
command = ['mysql', '-uUSER', '-pPWD','-h192.168.1.132', '--database=dbName' ] with open('./1477597236_foo.sql') as input_file: proc = subprocess.Popen( command, stdin = input_file, stderr=subprocess.PIPE, stdout=subprocess.PIPE ) output,error = proc.communicate()
(I added the next line which should be a
communicate call: since both stdout & stderr are redirected, it's the only simple way to avoid deadlocks between both output streams)