user1336958
user1336958

Reputation: 105

Calling rsync from Python versus bash script

I have an rsync call that I've moved from a bash script into a python script. Strangely, the rsync is having issues when called from python:

Here's the bash call:

 rsync --delete --exclude .svn -avz /home/app/resources/$RESOURCES_TO_UPDATE /home/elc/app/omap3/$RESOURCES_TO_UPDATE/resources

Here's the Python call:

 os.system("rsync --delete --exclude .svn -avz /home/app/resources/$RESOURCES_TO_UPDATE /home/elc/app/omap3/$RESOURCES_TO_UPDATE/resources")

What am I missing?

Upvotes: 0

Views: 4085

Answers (2)

boatcoder
boatcoder

Reputation: 18127

You could rewrite the call like so:

resources_to_update = os.environ["RESOURCES_TO_UPDATE"]
os.system("rsync --delete --exclude .svn -avz /home/app/resources/%s /home/elc/app/omap3/%s/resources" % (resources_to_update, resources_to_update))

or for 2.7+

os.system("rsync --delete --exclude .svn -avz /home/app/resources/{0} /home/elc/app/omap3/{0}/resources".format(os.environ["RESOURCES_TO_UPDATE"]))

Upvotes: 1

Kaelin Colclasure
Kaelin Colclasure

Reputation: 3975

My first suspicion would be that the environment variables you are referencing are not set in the shell spawned for the os.system call. You might try debugging by making Python spawn an echo command in order to verify that the results are what you expect.

os.system("echo $RESOURCES_TO_UPDATE")

If the environment variable is present you should see its contents printed.

Upvotes: 4

Related Questions