python - using subprocess.call with mysqldump -
i have been scripting windows many years , have started @ python alternative in last few weeks. i'm trying write native python script backup mysql database mysqldump. command line piping output > without issue.
i see many answers subprocess.popen , shell=true, equally see many statements should avoid shell=true
so i'm trying following code redirect stdout file, without success
sys.stdout=open("mysqldump.txt",'w') print("testing line1") subprocess.check_output(["mysqldump", "-u", "usernmae", "-ppassword", "-h", "dbserver_name", database_name])
if comment out sys.sdout line see sqldump outputting screen know have syntax correct part. added print statement , can see gets written file mysqldump.txt. when run in full there no dump screen or file
any ideas? i'm trying avoid using shell solution
what tried doesn't work because modifying sys.stdout
affects python-level statements such print
, not lower-level writes c, , particularly not performed external program. want tell subprocess
create pipe, did >
redirection, goes this:
with open("mysqldump.txt",'w') out: subprocess.check_call(["mysqldump", "-u", "usernmae", "-ppassword", "-h", "dbserver_name", database_name], stdout=out)
Comments
Post a Comment