os.popen
已弃用。使用子流程模块。subprocess.call
将阻塞主进程,直到命令完成。您应该检查返回码 ,以防在执行命令retval
时出现错误。mysqldump
在这种情况下,您不能继续diff
:
import subprocess
import shlex
with open("{f}.sql".format(f=filename), 'w') as fout:
retval = subprocess.call(
shlex.split(
"""mysqldump --login-path=server1_mysql -e --opt --skip-lock-tables
--skip-extended-insert -c %s""" % (database, )),
stdout=fout)
if not retval == 0:
raise Exception('Error executing command: {r}'.format(r=retval))
else:
with open("{f}.PATCH".format(f=filename), 'w') as fout:
retval = subprocess.call(
shlex.split("diff {w} {f}".format(w=weekly, f=filename)),
stdout=fout)