Using pipe in a system call

Ian Kelly ian.g.kelly at gmail.com
Thu Oct 8 18:26:35 EDT 2015


On Thu, Oct 8, 2015 at 4:03 PM, Cecil Westerhof <Cecil at decebal.nl> wrote:
> 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?

That seems fine to me. Alternatively you could pass shell=True to
Popen and then the original command should work verbatim (but see the
warnings about using shell=True).



More information about the Python-list mailing list