简体   繁体   中英

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?

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.

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"]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM