Bill Cheatham
Bill Cheatham

Reputation: 11947

`subprocess.call` operates differently to running command directly in shell

I have the following command in Python, which I wrote with the aim of copying only .yaml files from a source directory (on the network) to a local target directory:

import subprocess as sp

cmd = ['rsync', '-rvt',  "--include='*/*.yaml'",  "--exclude='*/*'",
     source , destination]

print ' '.join(cmd)
sp.call(cmd)

However, when I run this Python, all files are copied, including .jpg etc.


When I run the shell command directly:

rsync -rvt --include='*/*.yaml' --exclude='*/*' <source> <target>

...then only .yaml files are copied, as expected.


What is going on here? Why does the command operate differently in shell than under subprocess.call?

(This is with a Bash shell on Ubuntu 14.04, using Anaconda's Python 2)

Upvotes: 1

Views: 179

Answers (1)

Lie Ryan
Lie Ryan

Reputation: 64953

You should remove the single quotes around the wildcards:

['rsync', '-rvt',  "--include=*/*.yaml",  "--exclude=*/*",     source , destination]

Those quotes are processed by the shell, the shell doesn't pass in the quotes to rsync.

Upvotes: 3

Related Questions