2016-10-01 68 views
1

我使用rsync將文件從家用計算機移動到服務器。以下是我用來傳輸和更新僅包含grep + glob的文件的目錄的命令。我從下面顯示的目錄結構的toplevel/目錄執行該命令。Rsync include/exclude在python子進程中不起作用

rsync -r --progress --include='**201609*/***' --exclude='*' -avzh files/ [email protected]:/user/files 

這裏就是我的家文件的工作目錄的文件結構如下:

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

這工作得很好,並在[email protected]:/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/", "[email protected]:/user/files"] 

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

這是怎麼回事?當command_run是一個字符串時,我遇到了同樣的問題,並且我通過shell=True將它傳遞給了subprocess.run()

+1

的報價是外殼,而不是rsync的運行的。 –

+0

是的,單引號被剝離。試試''--include = ** 201609 */***「'。 – tdelaney

回答

1

這些引號中的某些引號在被傳遞到被調用進程之前被shell刪除。如果您使用默認shell=False調用該程序,則需要自己執行此操作。這個小腳本會告訴你,你的參數需要像

test.py

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

然後用命令行

~/tmp $ ./test.py -r --progress --include='**201609*/***' --exclude='*' -avzh files/ [email protected]:/user/files 
['./test.py', '-r', '--progress', '--include=**201609*/***', '--exclude=*', '-avzh', 'files/', '[email protected]:/user/files'] 
~/tmp $ 
+0

從我的腳本中剝離單引號爲我修復它!感謝大家! – conchoecia