subprocess.Popen output to file?

J

John Pye

Hi all

I am trying to set up a python script to manage backups of a mysql
database the 'right way' using pipes. I want to send the output of the
'mysqldump' command to a file. Using a normal shell script this would
be easy using a ">" operator. What is the efficient and best way to do
this using pure python, bearing in mind that it will be too much data
to keep in memory? I presume using the subprocess module, but how to
get the output to a file? It's not really documented, AFAICS.

Cheers
JP
 
M

Marc 'BlackJack' Rintsch

I am trying to set up a python script to manage backups of a mysql
database the 'right way' using pipes. I want to send the output of the
'mysqldump' command to a file. Using a normal shell script this would
be easy using a ">" operator. What is the efficient and best way to do
this using pure python, bearing in mind that it will be too much data
to keep in memory? I presume using the subprocess module, but how to
get the output to a file? It's not really documented, AFAICS.

I think it is documented, that's what the `stderr` and `stdout` arguments
are used for. Untested:

f = open('dump.txt', 'wb')
p = Popen(('mysqldump', '--option', '--another-option'), stdout=f)
r = p.wait()

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top