views:

71

answers:

1

I am having some problems using subprocess.call to export a database using mysqldump. I'm using Python 3.1 installed on Windows 7.

from time import gmtime, strftime
import subprocess

DumpDir = "c:/apps/sqlbackup/";
DumpFile = "mysqldump-" + strftime("%Y-%m-%d-%H-%M-%S", gmtime()) + ".sql";

params = [r"mysqldump --user root --password=mypassword --force --flush-privileges --compress --comments mydatabase --result-file=" + DumpDir + DumpFile];

subprocess.call(params, shell=True);

The above code causes a blank file to be created in the DumpDir.

I've tried getting python to print the command so I can test it via the CMD prompt using:

print(subprocess.list2cmdline(params));

If I paste the output to the CMD prompt and execute it, everything works fine.

Any ideas?

I'm new to Python, so I am sure the answer is simple but I've tried so many variations to get this working that I just can't figure this out.

A: 

I'm uncertain of what exactly caused the problem but the above code now works after:

  1. I reset the PATH environment setting in Windows to be sure the path to mysqldump is added everytime the PC boots.
  2. I restarted the PC (to ensure Python/MySQL were restarted and PATH environment settings were definitely correct).

The problem appears to be resolved now, which is a little strange. The fact that the .sql file was being created but was empty indicated that mysqldump was executed by Python. I'm not sure why the output file was empty when executed by Python versus executing the same mysqldump command from the CMD prompt provided a successful export.

caldazar