سؤال

I am trying to to run rsync for each folder in a folder.

__author__ = 'Alexander'
import os
import subprocess

root ='/data/shares'
arguments=["--verbose", "--recursive", "--dry-run", "--human-readable", "--remove-source-files"]
remote_host = 'TL-AS203'

for folder in os.listdir(root):
    print 'Sync Team ' + folder.__str__()

    path = os.path.join(root,folder, 'in')
    if os.path.exists(path):
        folder_arguments = list(arguments)
        print (type(folder_arguments))
        folder_arguments.append("--log-file=" + path +"/rsync.log")
        folder_arguments.append(path)
        folder_arguments.append("transfer@"+remote_host+":/data/shares/"+ folder+"/out")
        print "running rsync with " + str(folder_arguments)
        returncode = subprocess.call(["rsync",str(folder_arguments)])
        if returncode == 0:
            print "pull successfull"
        else:
            print "error during rsync pull"
    else:
        print "not a valid team folder, in not found"

If I run this I get the following output:

Sync Team IT-Systemberatung
<type 'list'>
running rsync with ['--verbose', '--recursive', '--dry-run', '--human-readable', '--remove-source-files', '--log-file=/data/shares/IT-Systemberatung/in/rsync.log', '/data/shares/IT-Systemberatung/in', 'transfer@TL-AS203:/data/shares/IT-Systemberatung/out']
rsync: change_dir "/data/shares/IT-Systemberatung/['--verbose', '--recursive', '--dry-run', '--human-readable', '--remove-source-files', '--log-file=/data/shares/IT-Systemberatung/in/rsync.log', '/data/shares/IT-Systemberatung/in', 'transfer@TL-AS203:/data/shares/IT-Systemberatung" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1040) [sender=3.0.4]
error during rsync pull
Sync Team IT-Applikationsbetrieb
not a valid team folder, in not found
transfer@INT-AS238:/data/shares/IT-Systemberatung

If i manually start rsync from bash with these arguments, everything works fine. I also tried it with shell=true but with the same result.

هل كانت مفيدة؟

المحلول

You need to do:

returncode = subprocess.call(["rsync"] + folder_arguments)

Calling str() on a list will return the string represention of the python list which is not what you want to pass in as an argument to rsync

نصائح أخرى

You do a os.chdir(os.path.join(root,folder)), but never go back.

In order to properly resume operation on the next folder, you should either remember the last os.getpwd() and return to it, or just do os.chdir('..') at the end of one loop run.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top