Merlin
Merlin

Reputation: 25639

How execute script from command prompt in python

I have mysql dump command that I would like to run from from windows shell or command prompt. I have used shell it does work.

  d= 'BkSql_'+datetime.datetime.now().strftime("%Y-%m-%d")+".sql"
  fn = dn+d    
  cmd="""mysqldump -u hapopdy -p   > %s""" %fn
  print cmd

Edit::::::: The -p needs to be a raw input.

Upvotes: 0

Views: 501

Answers (2)

orip
orip

Reputation: 75427

Using the subprocess module

import subprocess
subprocess.call(cmd)

If you're running a shell command add shell=True

subprocess.call(cmd, shell=True)

Upvotes: 5

hayalci
hayalci

Reputation: 4109

You should save the password in mysql's local configuration file for the user.(In Unix it's ~/.my.cnf) or you can give it on the command line with --password=MYPASSWORD.

Either way, the password will be visible to a large audience. In the .my.cnf case, it will be visible to anyone with read access to the file. In the second case, it will be visible to anyone who can get a process listing on the system, in addition to those who have read access to your script.

Upvotes: 0

Related Questions