簡體   English   中英

Python管道模塊如何做“貓”

[英]How would the Python pipes module do a 'cat'

我試圖在python中這樣做:

cat foo | ssh me@xxxx hadoop fs -put - bar/foo

我最初嘗試過check_call:

foo = 'foo'
subprocess.check_call(['cat', foo, '|','ssh',os.environ['USER']+'@'+hadoopGateway,'hadoop','fs','-put', '-', inputArgs.targetDir+'/'+foo])

產生錯誤:

cat: invalid option -- 'p'

我已經查看了python管道模塊文檔,並在shell中使用它,但我不明白如何在沒有輸出文件的情況下啟動它,如示例。

>>> t = pipes.Template()
>>> t.prepend('cat foo', '.-')
>>> t.append('hadoop fs -put - bar/foo', '-.') # what next

顯然我錯過了一些東西。

你不需要cat或管道; 您只需要提供文件作為ssh命令的標准輸入。 在shell中,那就是

ssh ${USER}@${hadoopGateway} hadoop fs -put - ${targetDir}/foo < foo

並且使用Python子進程模塊,它只涉及更多:

foo='foo'
subprocess.check_call(['ssh',
                       os.environ['USER']+'@'+hadoopGateway,
                       'hadoop', 'fs', '-put', '-', inputArgs.targetDir+'/'+foo],
                      stdin=open(foo, 'r'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM