Python子进程调用rsync

Python子进程调用rsync,python,rsync,Python,Rsync,我正在尝试为文件夹中的每个文件夹运行rsync __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(roo

我正在尝试为文件夹中的每个文件夹运行rsync

__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"
如果运行此命令,我将获得以下输出:

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
同步团队IT系统
使用['--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“失败:没有这样的文件或目录(2)
rsync错误:main.c(1040)[sender=3.0.4]中的某些文件/属性未被传输(请参阅以前的错误)(代码23)
rsync拉取期间出错
同步团队IT应用程序数据库
不是有效的团队文件夹,在中找不到
transfer@INT-AS238:/data/shares/IT系统
如果我用这些参数从bash手动启动rsync,一切正常。我也用shell=true尝试了它,但结果相同。

您执行了一个
os.chdir(os.path.join(root,folder))
,但从未返回

为了正确地恢复对下一个文件夹的操作,您应该记住最后一个
os.getpwd()
并返回到它,或者在一次循环运行结束时执行
os.chdir(“..”)

您需要执行以下操作:

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

对列表调用
str()
将返回python列表的字符串表示形式,这不是您想要作为参数传递给
rsync

对不起,这是代码中的一个遗留问题,不要再使用os.chdir了,谢谢,这解决了我的问题!我不明白为什么我需要使用(([“rsync”]+文件夹参数)([“rsync”],“folder_arguments”?
subprocess.call
要求整个命令行有一个列表。
+
操作符将两个列表合并到一个列表中。由于folder_arguments已经是一个列表,只需将其附加到仅包含“rsync”的列表中即可。因此,生成的列表看起来像:[“rsync”、“--verbose”、“--recursive”、…]