subprocess.Popen output to file?

Discussion in 'Python' started by John Pye, Apr 7, 2007.

  1. John Pye

    John Pye Guest

    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
    John Pye, Apr 7, 2007
    #1
    1. Advertising

  2. In <>, John Pye wrote:

    > 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
    Marc 'BlackJack' Rintsch, Apr 7, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Clodoaldo Pinto Neto

    Output from subprocess.Popen()

    Clodoaldo Pinto Neto, Oct 15, 2006, in forum: Python
    Replies:
    9
    Views:
    812
    Clodoaldo Pinto Neto
    Oct 17, 2006
  2. sven _
    Replies:
    7
    Views:
    4,168
    Thomas Dimson
    Apr 10, 2008
  3. Replies:
    2
    Views:
    657
    Matt Nordhoff
    Jul 10, 2008
  4. Replies:
    4
    Views:
    418
  5. Ashok Prabhu
    Replies:
    1
    Views:
    985
    Nobody
    Feb 4, 2010
Loading...

Share This Page