Home > Software design >  How can I read the error content outputs of mysql command executed from python
How can I read the error content outputs of mysql command executed from python

Time:12-15

bash_fc = rf"mysql -u {source_user_name} -h {source_ipv4_addr} --password={source_db_password}"

When I use the following functions for the above command,

from subprocess import PIPE,run
def cons_r(cmd):
  response = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
  return response.stdout
response = os.popen(bash_fc)

mysql: [Warning] Using a password on the command line interface can be insecure. mysql: Unknown OS character set 'cp857'. mysql: Switching to the default character set 'utf8mb4'. ERROR 1045 (28000): Access denied for user 'root'@'pc.mshome.net' (using password: YES)

I can't read the output, is there a method you know of so I can read it?

CodePudding user response:

This change fixed the problem

from subprocess import PIPE, run, STDOUT
def cons_r(cmd):
  response = run(cmd, stdout=PIPE, stderr=STDOUT, universal_newlines=True, shell=True)
  return response.stdout```

CodePudding user response:

You can read errors from standard error

import subprocess as sp

ret = sp.run(['ls', '*.bad'], stderr=sp.PIPE,
             stdout=sp.PIPE, shell=True, encoding="cp857")

if ret.returncode == 0:
    print(ret.stdout)
else:
    print(ret.stderr)
  • Related