【发布时间】:2016-10-19 02:14:19
【问题描述】:
我正在尝试使用pexpect 来rsync 一些文件。我提供的用于识别所有源文件的 glob 字符串参数似乎不起作用。
它的要点是这样的......
import pexpect
import sys
glob_str = (
"[0-9]" * 4 + "-" +
"[0-9]" * 2 + "-" +
"[0-9]" * 2 + "-" +
"[A-B]" + "*"
)
SRC = "../data/{}".format(glob_str)
DES = "user@host:" + "/path/to/dest/"
args = [
"-avP",
SRC,
DES,
]
print "rsync" + " ".join(args)
# Execute the transfer
child = pexpect.spawn("rsync", args)
child.logfile_read = sys.stdout # log what the child sends back
child.expect("Password:")
child.sendline("#######")
child.expect(pexpect.EOF)
失败了...
building file list ...
rsync: link_stat "/Users/U6020643/git/ue-sme-query-logs/code/../data/[0-9][0-9][0-9][0-9]\-[0-9][0-9]\-[0-9][0-9]\-[A-B]*" failed: No such file or directory (2)
0 files to consider
...
在 shell 中运行相同的命令也可以正常工作
rsync -avP ../data/[0-9][0-9][0-9][0-9]\-[0-9][0-9]\-[0-9][0-9]\-[A-B].csv username@host:/path/to/dest/
pexpect documentation 提到了这一点
请记住,Pexpect 不会解释 shell 元字符,例如重定向、管道或通配符(>、| 或 *)。这是一个常见的错误。如果你想运行一个命令并通过另一个命令传递它,那么你还必须启动一个 shell。
但是这样做...
...
args = [
"rsync",
"-avP",
SRC,
DES,
]
...
child = pexpect.spawn("/bin/bash", args) # have to use a shell for glob expansion to work
...
遇到权限问题
/usr/bin/rsync: /usr/bin/rsync: cannot execute binary file
【问题讨论】:
标签: python-2.7 shell glob pexpect