empty stdout (subprocess.run)

Cameron Simpson cs at cskk.id.au
Wed Jan 19 23:13:59 EST 2022


On 19Jan2022 19:16, James Smith <bjlockie021 at gmail.com> wrote:
>I'm trying to run a shell command but the stdout is empty:
>
>import subprocess
>
>torrentno=8
>cmd="/usr/bin/transmission-remote --torrent %s --info", str(torrentno)
>res=subprocess.run(cmd, shell=True, check=True, universal_newlines=True, capture_output=True)
>print(res)
>
>CompletedProcess(args=('/usr/bin/transmission-remote --torrent %s --info', '1'), returncode=0, stdout='', stderr='')

If you're using shell=True (please don't) you want cmd to be a string.  
You have defined it as a 2-tuple. You can even see that in the 
CompletedProcess object you printed.

I think you want to % substitue torrentno into the format string.

But I recommend you use shell=False and make:

    cmd = ["/usr/bin/transmission-remote", "--torrent", str(torrentno), "--info"]

because assembling commands with % formatting is a recipe for injection 
attacks. Look up "Little Bobby Tables", which is an SQL example of what 
you're doing with the shell.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list