I need to run an rsync command from Python. Is this possible and if so, how do I do it?
rsync -Ccavz --delete DJStatic username@website
You can call a subprocess from python using the following snippet
import subprocess
subprocess.call(["ls", "-l"])
In your case, it would be something like this
subprocess.call(["rsync", "-Ccavz", "--delete","DJStatic", "username@website"])
See here for more details.
subprocess.call("rsync -Ccavz --delete DJStatic username@website".split())
is slightly more readable and practical to type.
shlex.split(s)
instead of regular s.split()
to split arguments up as a normal (POSIX) shell would.
Commented
Mar 27, 2020 at 17:02
shlex.split()
instead of split()
, as mentioned by Chris L. Barnes. I think it's fine to use that solution if you do so cautiously. Unless...is there some other specific issue this causes?
fabric
provides a nice api that wraps many comman shell commands docs.fabfile.org/en/1.4.0/index.html