Success:
>>> scp_cmd = r"sudo scp -i /home/backup/.ssh/id_rsa /opt/backups/*conf backup@a-hostname.local:/opt/backups/"
>>> subprocess.call(scp_cmd, shell=True)
1eadmin1.conf 100% 83KB 83.5KB/s 00:00
1stflr_1.conf 100% 2904 2.8KB/s 00:00
>>> scp_cmd = """sudo scp -i /home/backup/.ssh/id_rsa /opt/backups/*conf backup@a-hostname.local:/opt/backups/"""
>>> os.system(scp_cmd)
1eadmin1.conf 100% 83KB 87.3KB/s 00:00
1stflr_1.conf 100% 2904 3.4KB/s 00:00
Failure:
>>> scp_cmd = r"""sudo scp -i /home/backup/.ssh/id_rsa /opt/backups/*conf backup@a-hostname.local:/opt/backups/"""
>>> subprocess.call(scp_cmd, shell=True)
/opt/backups/*conf: No such file or directory
1
>>> subprocess.call(scp_cmd.split(' '))
/opt/backups/\*conf: No such file or directory
1
>>>
>>> subprocess.call(shlex.split(scp_cmd))
/opt/backups/*conf: No such file or directory
1
I’m confused why the triple quotes fail when I use subprocess.call(), but pass when I use os.system(). Why is there a difference between subprocess.call() and os.system() when handling triple quoted strings?
I am pretty certain you are doing something else different; the triple quoting isn’t making a difference here at all:
Using triple quoting is just one way to specify a python string literal. How you specified that literal (with or without the
rraw prefix, with single or triple quotes, using single'or double"quotes) is not preserved.Where triple quoting does make a difference is when you include a newline:
But your examples do not include any newlines at all.