python - Rsync 包含/排除在 python 子进程中不起作用

标签 python python-3.x rsync

我使用 rsync 将文件从我的家庭计算机移动到服务器。这是我用来传输和更新仅包含 grep + glob 的文件的目录的命令。我从下面显示的目录结构中的 toplevel/ 目录执行此命令。

 rsync -r --progress --include='**201609*/***' --exclude='*' -avzh files/ user@server.edu:/user/files

这是我的主文件中工作目录的文件结构:

 - toplevel
      - items
      - files
        - 20160315
        - 20160910
          - dir1
            - really_cool_file1
        - 20160911
          - dir2

这工作正常,user@server.edu:/user/files 上的文件结构与我家用电脑上的相同。


我写了一个 python 脚本来执行此操作,但它不起作用。它还通过 files/20160315 传输,这不是我想要的。

 #!/usr/bin/env python3
 import os
 from subprocess import run

 os.chdir("toplevel")

 command_run = ["rsync", "-r",
           "--progress",
           "--include='**201609*/***'",
           "--exclude='*'",
           "-avzh",
           "files/", "user@server.edu:/user/files"]

 run(command_run, shell=False, check=True)

这是怎么回事?当 command_run 是一个字符串时,我遇到了同样的问题,我使用 shell=True 将它传递给 subprocess.run()

最佳答案

其中一些引号在传递给被调用进程之前被 shell 删除。如果您使用默认的 shell=False 调用程序,则需要自己执行此操作。这个小脚本会告诉你你的参数需要是什么样的

测试.py

#!/usr/bin/env python3
import sys
print(sys.argv)

然后用你的命令行运行

~/tmp $ ./test.py -r --progress --include='**201609*/***' --exclude='*' -avzh files/ user@server.edu:/user/files
['./test.py', '-r', '--progress', '--include=**201609*/***', '--exclude=*', '-avzh', 'files/', 'user@server.edu:/user/files']
~/tmp $

关于python - Rsync 包含/排除在 python 子进程中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39801862/

相关文章:

python - Pandas Seaborn Swarmplot 不绘图

python - 为什么 python decode 替换了编码字符串中的无效字节?

具有大型数组的 Windows 上的 Python 多处理

python - 基于groupby的绘图

python - 如何注释 f(*params)?

python - 如何从多个列表生成字典?

linux - 如何通过rsync只复制符号链接(symbolic link)

python - 如何将 Pandas 系列中的连续 NaN 值分组到一组切片中?

linux - 来自 bash 脚本端口故障的 rsync

ssh - StrictHostKeyChecking 不忽略指纹验证