2

I tried to to that like:

execfile('c:/xampp/mysql/bin/mysqldump -h localhost -u ' + username + ' -p' + password + ' ' + database + ' > ' + projectname + '.sql');

Then I see an error:

IOError: [Errno 2] No such file or directory: 'c:/xampp/mysql/bin/mysqldump -h localhost -u root -p pr > pr.sql'

What I do wrong?

I trie to write output to file like:

output = subprocess.check_output([r'c:\xampp\mysql\bin\mysqldump', '-h', 'localhost', '-u', username, '--password=', database], stdout=open(projectname + '.sql','w'))

It does not work

4
  • Are you on Windows ? Commented Nov 12, 2017 at 20:44
  • What does it say if you just paste the full command into the terminal and cut out python? It's seems like its a bad path to one of the files and not a python issue. Commented Nov 12, 2017 at 20:44
  • Yes under Windows Commented Nov 12, 2017 at 21:09
  • How to write output in file? I tried, see question Commented Nov 12, 2017 at 22:11

2 Answers 2

4

execfile doesn't take a whole command, but the name of the file to execute as the first argument. Moreover it's a way of calling other Python program, not a generic program - like mysqldump.

I suggest you use subprocess.call instead, e.g.

subprocess.call(['c:/xampp/mysql/bin/mysqldump', '-h', 'localhost', '-u', username, '-p' + password, database])

And then you need to take care of the redirection (typically done by your shell if you run it manually), so the full solution would be:

f = open(projectname+".sql", "w")
subprocess.call(['c:/xampp/mysql/bin/mysqldump', '-h', 'localhost', '-u', username, '-p' + password, database], stdout=f)
Sign up to request clarification or add additional context in comments.

4 Comments

You missed parameter here password, database]).
@OPV, not sure I get your comment. -pyoursecret is a single parameter (no spaces within). As opposed to -h, localhostwhich are two parameters. I haven't checked what mysqldump takes, but following your attempt.
Man, where is remaining part of command I mean file in which I put data? Like > db_backup.sql
@OPV - saving the output to .sql file is described in the last paragraph of my answer (referred to as 'redirection').
2

execfile is designed to run python files only (the name is misleading).

Plus it has been removed from Python 3...

For your issue, just use subprocess.check_output, splitting the arguments properly so quoting is handled automatically (never compose your command line by yourself). Here the -p option must have the password without spaces (that's a command specificity), or use --password which is clearer

output = subprocess.check_output([r'c:\xampp\mysql\bin\mysqldump', '-h', 'localhost', '-u', username, '--password', password, database])

Note: it seems that your password is blank (empty string), in that case, just use --password= like explained here

output = subprocess.check_output([r'c:\xampp\mysql\bin\mysqldump', '-h', 'localhost', '-u', username, '--password=', database])

now the output of your command is in output (if the command succeeds), you can write it into a file (well, in your case, it's true that you'be be better off with check_call and stdout=file_handle because you seem to need the .sql file, not only the buffer) anyway:

with open("file.sql","w") as f:
   f.write(output)

9 Comments

Where do you put parameter database?
database is the last parameter of the command, right after password. It's there all right.
For what there is output?
output is the contents of the sql file as python string. Sometimes creating a file isn't needed when you want to process the output from python (saves the reading of the file). Doesn't seem to be your case, though.
When I execute this subprocess.call(['c:/xampp/mysql/bin/mysqldump', '-h', 'localhost', '-u', username, '-p' + password, database]) it requires to enter a password in console. How to avoid that?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.