【问题标题】:`subprocess.call` operates differently to running command directly in shell`subprocess.call` 的操作方式与直接在 shell 中运行命令不同
【发布时间】:2016-03-17 10:39:13
【问题描述】:

我在 Python 中有以下命令,我编写该命令的目的是仅将 .yaml 文件从 source 目录(在网络上)复制到本地 target 目录:

import subprocess as sp

cmd = ['rsync', '-rvt',  "--include='*/*.yaml'",  "--exclude='*/*'",
     source , destination]

print ' '.join(cmd)
sp.call(cmd)

但是,当我运行这个 Python 时,所有文件都被复制了,包括 .jpg 等。


当我直接运行shell命令时:

rsync -rvt --include='*/*.yaml' --exclude='*/*' <source> <target>

...那么只有.yaml 文件被复制,正如预期的那样。


这里发生了什么?为什么命令在 shell 中的操作与在 subprocess.call 下的操作不同?

(这是在 Ubuntu 14.04 上使用 Bash shell,使用 Anaconda 的 Python 2)

【问题讨论】:

    标签: python shell subprocess rsync


    【解决方案1】:

    您应该删除通配符周围的单引号:

    ['rsync', '-rvt',  "--include=*/*.yaml",  "--exclude=*/*",     source , destination]
    

    这些引号由shell处理,shell不会将引号传递给rsync。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      相关资源
      最近更新 更多