Using pipe in a system call

Cecil Westerhof Cecil at decebal.nl
Thu Oct 8 18:03:36 EDT 2015


I want to do the following Bash command in Python:
    sqlite3 spreekwoorden.sqlite "SELECT spreekwoord FROM spreekwoorden;" | sort > spreekwoorden2.txt

The following does this in Python:
    sqlite_pipe = Popen(
        (
            'sqlite3',
            'spreekwoorden.sqlite',
            'SELECT spreekwoord FROM spreekwoorden;'
        ),
        stdout = PIPE
    )
    Popen(
        (
            'sort',
            '--output=spreekwoorden2.txt',
        ),
        stdin = sqlite_pipe.stdout
    )

Is this the correct way, or is there a better way?


By the way: I want this because I just filled the table spreekwoorden
in my Python script and want to save it immediately.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



More information about the Python-list mailing list