2016-11-22 30 views
2

私はPythonでファブリックパッケージを使用して、さまざまなHDFSタスクのシェルスクリプトを実行してきました。ファイルがPythonのHDFSに存在するか確認してください

しかし、ファイル/ディレクトリがHDFSに既に存在するかどうかを確認するタスクを実行するときは、シェルを終了するだけです。ここで私はまたlocal('hadoop fs -test -e hdfs://some/nonexistent/hdfs/dir/')が、それを試してみました

[localhost] local: hadoop fs -stat hdfs://some/nonexistent/hdfs/dir/ stat: `hdfs://some/nonexistent/hdfs/dir/': No such file or directory

Fatal error: local() encountered an error (return code 1) while executing 'hadoop fs -stat hdfs://some/nonexistent/hdfs/dir/'

Aborting.

例ディレクトリが存在しない場合は、

from fabric.api import local 


local('hadoop fs -stat hdfs://some/nonexistent/hdfs/dir/') 

(Iは、Python 3.5.2とFabric3の== 1.12.post1を使用しています)このコード収量があります同じ問題が発生しました。

ファブリックを使用して、ディレクトリまたはファイルがhdfsに存在するかどうかを示すブール変数を生成するにはどうすればよいですか?

答えて

1

localから返される結果オブジェクトのsucceededフラグをチェックすることができます。

from fabric.api import local 
from fabric.context_managers import settings 

file_exists = False 
with settings(warn_only=True): 
    result = local('hadoop fs -stat hdfs://some/nonexistent/hdfs/dir/', capture=True) 
    file_exists = result.succeeded 
関連する問題